2015-10-21 28 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