2016-02-04 6 views
7

渡されるすべての型のタプルを持つクラスを作成しようとしています。私はそれがテンプレートの型として型リストを取り、内部タプルに含まれるクラスとしてそのリスト内のクラスを使用します。現在、私は実際にコンパイルしないようなものを持っています。タプルに展開する型リストを作成するにはどうすればよいですか?

template<class ... T> 
struct ComponentList {}; 

template<ComponentList<typename ...T> > 
class ComponentManager{ 
    std::tuple<T...> components; 
}; 

私はまた後で他のタイプのリストを渡したいので、私はComponentListは、独自の型で持ちたい理由があります。これは可能ですか?そうでない場合は、何が代わりになるでしょうか?

答えて

6

あなたはstd::tupleにあなたのタイプレベルのリストからパラメータを再バインドするためのテンプレートを追加することができます:

template<class A, template<class...> class B> 
struct rebind_; 

template<template<class...> class A, class... T, template<class...> class B> 
struct rebind_<A<T...>, B> { 
    using type = B<T...>; 
}; 

template<class A, template<class...> class B> 
using rebind = typename rebind_<A, B>::type; 

template <class ... T> 
struct ComponentList { 
    typedef std::tuple<T...> tuple_t; 
}; 

template<class T> 
class ComponentManager { 
    typename T::tuple_t components; 
}; 

使い方はあなたが期待するものですそのように使用してください:

template<class... T> 
struct ComponentList {}; 

template<class List> 
struct ComponentManager { 
    rebind<List, std::tuple> components; 
}; 

int main() { 
    using List = ComponentList<int, char, long>; 
    ComponentManager<List> manager; 
    std::cout << std::get<0>(manager.components) << '\n'; 
} 

私はあなたが元の型がComponentListであることを強制したい場合、あなたはenable_ifis_instantiation_ofを使用することができますね。

template<class List, 
    typename = std::enable_if<is_instantiation_of<List, ComponentList>::value>::type> 
struct ComponentManager { 
    rebind<List, std::tuple> components; 
}; 
1

すべてのタイプリストクラスは内部型のtuple_tタイプを提供するように強制できますか?ような何か:

ComponentManager<ComponentList<int, double> > cm; 
+0

これは可能性があるが、私は提供されるクラスの施行タイプの両方のよりよい解決策を期待していました(タプルを作る代わりに、それぞれの型を型のベクトルにして、それをタプルの内側に貼り付けるなど)、型リストの中の型を操作することができるようにするために使用されます。 – user975989

関連する問題