从C++模板参数包编译时间数组

Mat*_*ias 6 c++ templates stl variadic-templates c++11

我怎样才能在编译时从模板参数包中创建一个std :: array?

这显示了我需要的东西,但没有参数包.

template<typename T1, typename T2, typename T3>
struct ToInfoArray
{
    static constexpr std::array<Info, 3> value = { { T1::info, T2::info, T3::info } };
};
Run Code Online (Sandbox Code Playgroud)

现场演示演示预期用途

奖金问题: 您会使用std::array,array[]还是std::initializer_listInfoArray的类型?

Pio*_*cki 7

template <typename... Ts>
struct ToInfoArray
{
    static constexpr std::array<Info, sizeof...(Ts)> value = { { Ts::info... } };
};
Run Code Online (Sandbox Code Playgroud)

DEMO

你会使用std :: array,array []或std :: initializer_list作为InfoArray的类型吗?

std::array<T,N>.它是一个聚合体,其行为(具有可比性能)就像常规数组一样,但提供了一个额外的接口来操作其元素; 本身,它是一个可复制的类型.

std::initializer_list由于种种原因,这里不是一种选择.一旦它被用作数据成员(使用类内初始化程序),它存储的元素在任何构造函数执行后都将失效.它不能保证是文字类型,因此不能标记为constexpr.它的大小在常量表达式中不可用(可访问).它不提供随机访问逻辑(不使用指针算法); 枚举其元素的唯一方法是迭代begin()end(),另外产生指向常量项的指针.std::initializer_list旨在主要作为功能参数.