2015-10-21 29 views
1

我需要collection_traits与我可以这样做如何确定C的一个实例化的容器模板的容器模板类型++

typename collection_traits<std::vector<int>>::template type<double> double_vector; 

现在double_vector是std::vector<double>类型。

所以我想要的是获得相同的容器,但具有不同的值类型。

这甚至可能吗?

+0

*现在double_vector的类型是性病载体:: * ???你是如何得出这个结论的? –

+0

你试图做什么并不是很清楚。获取相同类型的集合,但使用不同的value_type?此外,您不希望使用这种情境,因此难以给出任何合理的答案。 – Rostislav

+3

你期望为'std :: set'做什么? '的std :: map'?如果有自定义比较器会怎么样? – Barry

回答

3

Variadic template将帮助您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; 

} 
+0

这就是我一直在寻找的。我几乎到了那里,但别名模板在我的尝试中失踪。大。我知道了。 – user1415913