问题
I'd like to achieve something like that:
#include <string>
#include <array>
enum class MyEnum{
A,
B,
C
};
template<MyEnum... Args>
class MyClass{
public:
MyClass()
{
}
private:
std::array<MyEnum, sizeof...(Args)> array;
};
Now I have an array, which can hold all passed to template values. But how can I populate this array with template parameters?
回答1:
If what you are wanting is to put all the MyEnum
values into array
, then you can unpack them into an initialiser list and initialise initialise it with direct initialisation:array
with it
MyClass() : array {{ Args... }} { }
You need a fairly new compiler to use this syntax, however.
Thanks to Xeo for correcting my answer.
回答2:
MyClass()
{
std::initializer_list<MyEnum> il( {Args...} );
std::copy (il.begin(), il.end(), array.begin());
}
来源:https://stackoverflow.com/questions/10364874/create-stdarray-from-variadic-template