如何确定 C++ 中实例化容器模板的容器模板类型
How can I determine the container template type of an instantiated container template in C++
我需要 collection_traits 来做类似
的事情
typename collection_traits<std::vector<int>>::template type<double> double_vector;
现在 double_vector 的类型是 std::vector<double>
。
所以我想要的是获得相同的容器,但具有不同的值类型。
这可能吗?
Variadic template will help you https://ideone.com/lbS2W3
using namespace std;
template<typename... T>
struct collection_traits { using template_type = void; };
template<template<typename...> class C, typename... Args>
struct collection_traits<C<Args...> > {
template<typename... Subst>
using template_type = C<Subst...>;
};
int main(int argc, char *argv[]) {
collection_traits<vector<int>>::template_type<double> t;
cout << typeid(t).name() << endl;
t.push_back(123.5);
cout << t.front() << endl;
}
我需要 collection_traits 来做类似
的事情typename collection_traits<std::vector<int>>::template type<double> double_vector;
现在 double_vector 的类型是 std::vector<double>
。
所以我想要的是获得相同的容器,但具有不同的值类型。
这可能吗?
Variadic template will help you https://ideone.com/lbS2W3
using namespace std;
template<typename... T>
struct collection_traits { using template_type = void; };
template<template<typename...> class C, typename... Args>
struct collection_traits<C<Args...> > {
template<typename... Subst>
using template_type = C<Subst...>;
};
int main(int argc, char *argv[]) {
collection_traits<vector<int>>::template_type<double> t;
cout << typeid(t).name() << endl;
t.push_back(123.5);
cout << t.front() << endl;
}