
Say I have: template<typename in_t> void F (in_t & in) ... main() { F<std::vector<int> > (...) Suppose in_t is a templated container, such as std::vector<int>. Is there a way within the function 'F' to get the generic container type, 'std::vector' when F is instantiated with a specific e.g., std::vector<int>? Maybe I'm missing something obvious :)

It's not easy but there are a couple of techniques you could look at. The simplest is to overload your template function for the various containers of interest. It's not particularly general but it's easy. So, for instance, template < typename T > void F(std::vector<T>& ) { ... } would detect the std::vector container. Note that this template can call the more generic F template so you can get some code re-use. Another technique would be to decide to support only STL compliant containers and overload the template in a way that uses T::value_type to get the contained value type, depending on SFINAE to default back to the generic base template. This is more general but doesn't provide knowledge of the container type as the previous style does. At 06:49 AM 2/19/2009, Neal Becker wrote:
Say I have:
template<typename in_t> void F (in_t & in) ...
main() { F<std::vector<int> > (...)
Suppose in_t is a templated container, such as std::vector<int>. Is there a way within the function 'F' to get the generic container type, 'std::vector' when F is instantiated with a specific e.g., std::vector<int>?
Maybe I'm missing something obvious :)
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

2009/2/19 Neal Becker <ndbecker2@gmail.com>
Say I have:
template<typename in_t> void F (in_t & in) ...
main() { F<std::vector<int> > (...)
Suppose in_t is a templated container, such as std::vector<int>. Is there a way within the function 'F' to get the generic container type, 'std::vector' when F is instantiated with a specific e.g., std::vector<int>?
Maybe this will help you. template <class Base, class Arg> struct rebind; template <template <class> class Base, class T, class Arg> struct rebind<Base<T>, Arg> { typedef Base<Arg> type; }; template <class In> void foo() { // d is of type container<double>. typename rebind<In, double>::type d; } template <class T> struct container {}; int main() { foo<container<int> >(); } With vector and other STL containers it's a bit harder, because they can have more than one template parameter. Roman Perepelitsa.
participants (3)
-
Alan M. Carroll
-
Neal Becker
-
Roman Perepelitsa