Boost logo

Boost Users :

Subject: Re: [Boost-users] OT: generic template type from specific type?
From: Alan M. Carroll (amc_at_[hidden])
Date: 2009-02-20 23:29:47


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_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net