C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template templates

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Scott Frazer
Guest





PostPosted: Thu Sep 22, 2005 8:53 am    Post subject: Template templates Reply with 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<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





PostPosted: Thu Sep 22, 2005 11:33 am    Post subject: Re: Template templates Reply with quote



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





PostPosted: Thu Sep 22, 2005 2:21 pm    Post subject: Re: Template templates Reply with quote




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





PostPosted: Thu Sep 22, 2005 7:06 pm    Post subject: Re: Template templates Reply with quote


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





PostPosted: Sat Sep 24, 2005 2:07 am    Post subject: Re: Template templates Reply with quote


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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.