是否可以在 C++ 中提取容器模板 class?

Is it possible to extract in c++ the container template class?

我想知道是否可以检测模板 class 容器类型,并重新定义其参数。例如:

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

vdouble 现在是 std::vector<double>?

是的,您可以使用偏特化制作一个简单的模板重新绑定器:

#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 T1 = std::vector<int>;

using T2 = vector_rebinder<T1>::rebind<double>;

现在 T2std::vector<double>

添加到@Kerrek SB 的回答中,这是通用方法:

template<typename...> struct rebinder;

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

这适用于阳光下的任何容器。