I need to initialize a compile-time sized class array with an initializer_list. I'm already aware that I could use a parameter pack constructor and initialize it on the spot, but I need to use an initializer_list in this case. I'd also like to avoid dynamically initializing the array if possible. Here's the pseudo-code:
template<typename Type, std::size_t Length>
class Test
{
public:
Test(const std::initializer_list<Type> args)
: m_tData(args) //<-- Doesn't work of course
{}
private:
Type m_tData[Length];
};
Of course, for non-const types, I could do
Test(const std::initializer_list<Type> args)
{
std::copy(args.start(), args.end(), m_tData);
}
but this doesn't work if I try to use a const class like Test<const int, 3>({1, 2, 3})
. Am I forced to use template specialization to have the internal array not actually be const so I can initialize it from the constructor body?
I'm using C++20. I've seen How do I initialize a member array with an initializer_list? but was wondering if anything has changed since then.