Is it possible to extract a container template class in C ++?

I was wondering if it is possible to determine the container type of the template class and override its parameters. For instance:

typedef std::vector<int> vint; typedef typeget<vint>::change_param<double> vdouble; 

Where will vdouble be std::vector<double> ?

+6
source share
2 answers

Adding to @Kerrek SB's answer, here is a general approach:

 template<typename...> struct rebinder; template<template<typename...> class Container, typename ... Args> struct rebinder<Container<Args...>>{ template<typename ... UArgs> using rebind = Container<UArgs...>; }; 

That will work for any container under the sun.

+10
source

Yes, you can do a simple enumeration of templates using partial specialization:

 #include <memory> #include <vector> template <typename> struct vector_rebinder; template <typename T, typename A> struct vector_rebinder<std::vector<T, A>> { template <typename U> using rebind = std::vector<U, typename std::allocator_traits<A>::template rebind_alloc<U>>; }; 

Using:

 using T1 = std::vector<int>; using T2 = vector_rebinder<T1>::rebind<double>; 

Now T2 is std::vector<double> .

+7
source

All Articles