Help with deduced template parameters
Forgive me if the answer ends up being strictly C++, rather than Boost. But, I have a template class that I would rather be able to deduce it's single [current] template parameter rather than having to specify it. I'm wondering if boost::function or boost::functional might make this easier for me to accomplish. My functor class is passed to a method similar to std::for_each, and that template functor class is: /*! Detach all members of the EntityList via stop_messaging. * \todo I suspect there's a way to make this not require template * arguments at instantiation, but at the moment it's being used * as: * mutexed_for_each(detach_all<typeof(arg)>(arg)); */ template <typename List> class detach_all { public: detach_all(const List &e) : list(e) {} template <class T> void operator ()(T*ent) const { if (dynamic_cast<Messageable*>(ent)) { std::for_each(list.begin(), list.end(), stop_messaging(dynamic_cast<Messageable*>(ent))); } } private: const List &list; }; If I make the class not be a template, but instead make the constructor a template member, it would be able to determine the template parameter List from the argument to the constructor. However, I'm then unable to keep a reference to the List argument to the constructor, which I need during the operator() invocation. Am I missing something about templates that I don't know? Or, is this just not possible with C++ templates, and in which case, can I use one of the boost libraries to help? Thanks much. - Chris
Chris Ross escreveu:
Am I missing something about templates that I don't know? Or, is this just not possible with C++ templates, and in which case, can I use one of the boost libraries to help?
Normally in those cases you could create a function to determine the template parameters and pass it to the class. The function then returns a class object correctly instantiated, for instance: (say you renamed your class to detached_all_t) template <class List> detach_all_t<List> detach_all(const List &e) { return detach_all_t<List>(e); } That should do the trick, you can use detach_all without specifying the template parameter. Regards, rod
Chris Ross:
Forgive me if the answer ends up being strictly C++, rather than Boost. But, I have a template class that I would rather be able to deduce it's single [current] template parameter rather than having to specify it. I'm wondering if boost::function or boost::functional might make this easier for me to accomplish.
...
template <typename List> class detach_all { public: detach_all(const List &e) : list(e) {} template <class T> void operator ()(T*ent) const { if (dynamic_cast<Messageable*>(ent)) { std::for_each(list.begin(), list.end(), stop_messaging(dynamic_cast<Messageable*>(ent))); } }
struct detach_all { typedef void result_type; template<class T, class L> void operator()( T * pt, L * pl ) const { if( Messageable * pm = dynamic_cast<...>( pt ) ) { std::for_each( pl->begin(), pl->end(), stop_messaging( pm ) ); } } }; int main() { List list; mutexed_for_each( boost::bind( detach_all(), _1, &list ) ); } is one alternative.
participants (3)
-
Chris Ross
-
Peter Dimov
-
Rodolfo Lima