我們有模板類:模板參數包屬性
template<int i>
class A
{
...
};
但如何聲明模板類的包裝:
template<int... is>
Pack
{
private:
A<is...> attrs;
};
或者HOWTO有類的集合?
我們有模板類:模板參數包屬性
template<int i>
class A
{
...
};
但如何聲明模板類的包裝:
template<int... is>
Pack
{
private:
A<is...> attrs;
};
或者HOWTO有類的集合?
使用std::tuple
,通過例如
#include <tuple>
template <int i>
class A
{ };
template <int... is>
class Pack
{ std::tuple<A<is>...> attrs; };
int main()
{
Pack<2,3,5,7,11,13> p;
}
另一種方式可以是通過繼承
template <int i>
class A
{ };
template <int... is>
class Pack : A<is>...
{ };
int main()
{
Pack<2,3,5,7,11,13> p;
}
我所知道的最好的方法是使用一個類型列表:
template<class...> struct type_list{};
template<int...Is>
using a_pack = type_list<A<Is>...>;
使用類型列表,執行tra非常簡單nsformation或 對每個成員進行操作。例如,讓我們用前面的代碼創建一個std:vector的type_list:
template<class> struct vectors_of;
template<class...As> struct vectors_of<type_list<As...>>{
using type=type_list<std::vector<As>...>;
};
using vectors_of_a = typename vectors_of<a_pack<1,2>>::type;
他們是關於類型列表的很多文檔。這是本書以來元程序員的基本工具之一:Modern C++ Design(它使用pre-C++ 11)。使用C++ 11,它更容易使用它。