How can I at compile time create for example an std::array from a template parameter pack?
This shows what I need, but without a parameter pack.
template<typename T1, typename T2, typename T3>
struct ToInfoArray
{
static constexpr std::array<Info, 3> value = { { T1::info, T2::info, T3::info } };
};
Live demo demonstrating the intended usage
Bonus question:
Would you use std::array
, array[]
or std::initializer_list
as type for InfoArray?
template <typename... Ts>
struct ToInfoArray
{
static constexpr std::array<Info, sizeof...(Ts)> value = { { Ts::info... } };
};
DEMO
Would you use std::array, array[] or std::initializer_list as type for InfoArray?
std::array<T,N>
. It's an aggregate that behaves (with comparable performance) just like a regular array, but provides an additional interface for operating on its elements; itself, it's a copyable type.
std::initializer_list
is not an option here for numerous reasons. Once it's used as a data member (with an in-class initializer), the elements it stores are invalidated after any of constructors' execution. It's not guaranteed to be a literal type, thus it can't be marked as constexpr
. Its size is not usable (accessible) in constant expressions. It doesn't provide random access logic (without resorting to pointer arithmetic); the only way to enumerate its elements is to iterate from begin()
to end()
, which in addition yields pointers to constant items. std::initializer_list
was designed to serve primarily as a function parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With