 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Scott Frazer Guest
|
Posted: Thu Sep 22, 2005 8:53 am Post subject: Template templates |
|
|
I want to create template functions to clear out containers of pointers
to objects. I started out with this for std::list
template<typename T>
class DeleteObjectPtr {
public:
void operator()(T* objPtr) const { delete objPtr; }
};
template<typename T>
void destroyListOfPtrs(std::list<T*>& l) {
std::for_each(l.begin(), l.end(), DeleteObjectPtr<T>());
l.clear();
}
which works fine. Now I want it to work for other containers such as
std::deque and so on, but I don't want to write a template function for
each one. So how do I replace the "std::list" part of the template
function? I think I need to use template template parameters, but all
the syntax combinations I've tried won't compile.
Thanks,
Scott
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
TonyO Guest
|
Posted: Thu Sep 22, 2005 11:33 am Post subject: Re: Template templates |
|
|
| Quote: | template<typename T
void destroyListOfPtrs(std::list
std::for_each(l.begin(), l.end(), DeleteObjectPtr<T>());
l.clear();
}
|
template<typename ContainerT>
void destroyListOfPtrs(ContainerT& container) {
std::for_each(container.begin(), container.end(),
DeleteObjectPtr<typename ContainerT::value_type>());
container.clear();
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kwikius Guest
|
Posted: Thu Sep 22, 2005 2:21 pm Post subject: Re: Template templates |
|
|
Scott Frazer wrote:
| Quote: | I want to create template functions to clear out containers of pointers
to objects. I started out with this for std::list
template<typename T
class DeleteObjectPtr {
public:
void operator()(T* objPtr) const { delete objPtr; }
};
template
void destroyListOfPtrs(std::list
std::for_each(l.begin(), l.end(), DeleteObjectPtr<T>());
l.clear();
}
which works fine. Now I want it to work for other containers such as
std::deque and so on, but I don't want to write a template function for
each one. So how do I replace the "std::list" part of the template
function? I think I need to use template template parameters, but all
the syntax combinations I've tried won't compile.
|
This is one way (There are several others too of course).
template<
template
typename T,
typename A // allocator
| Quote: |
void destroyListOfPtrs(Container<T*,A> & c) { |
std::for_each(c.begin(), c.end(),DeleteObjectPtr<T>());
c.clear();
}
regards
Andy Little
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
TonyO Guest
|
Posted: Thu Sep 22, 2005 7:06 pm Post subject: Re: Template templates |
|
|
TonyO wrote:
| Quote: | template<typename ContainerT
void destroyListOfPtrs(ContainerT& container) {
std::for_each(container.begin(), container.end(),
DeleteObjectPtr
container.clear();
}
|
Oops - you'll also need to modify DeleteObjectPtr<> to be templated on
the pointer type itself (not the object type):
template<typename PointerT>
class DeleteObjectPtr {
public:
void operator()(PointerT objPtr) const { delete objPtr; }
};
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kwikius Guest
|
Posted: Sat Sep 24, 2005 2:07 am Post subject: Re: Template templates |
|
|
Scott Frazer wrote:
| Quote: | I want to create template functions to clear out containers of pointers
to objects. I started out with this for std::list
template<typename T
class DeleteObjectPtr {
public:
void operator()(T* objPtr) const { delete objPtr; }
};
template
void destroyListOfPtrs(std::list
std::for_each(l.begin(), l.end(), DeleteObjectPtr<T>());
l.clear();
}
which works fine. Now I want it to work for other containers such as
std::deque and so on, but I don't want to write a template function for
each one. So how do I replace the "std::list" part of the template
function? I think I need to use template template parameters, but all
the syntax combinations I've tried won't compile.
|
You should look at :
http://www.boost.org/libs/smart_ptr/shared_ptr.htm
as an alternative approach to the problem above
regards
Andy Little
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|