I’m trying to learn something about initializer_list<>. I learned on c++03 so to me, it is new.
template <typename T>
union Matrix4
{
struct
{
T m00, m01, m02, m03;
T m10, m11, m12, m13;
T m20, m21, m22, m23;
T m30, m31, m32, m33;
};
T m[16];
T mm[4][4];
Matrix4(std::initializer_list<T> values)
{
if (values.size() != 16)
{
throw InvalidArgumentException;
}
std::copy(values.begin(), values.end(), m);
}
//error: Implicit instantiation of "Matrix4<float>" within its own definition.
static Matrix4<float> Identityf = {
1.f, 0, 0, 0,
0, 1.f, 0, 0,
0, 0, 1.f, 0,
0, 0, 0, 1.f
};
}
I get what the error is saying. What I don’t get is why it is saying it. That is why can’t the compiler handle this?
What I would like to accomplish is something like so:
typedef Matrix4<float> Matrix4f;
Matrix4f mat = Matrix4f::Identity;
Initializing the static member
IdentifyFneeds to call theMatrix4constructor, which implicitly instantiates theMatrix4<float>specialization, but at the point where it’s instantiated the class template isn’t (fully) defined yet.A class (or a class template) is defined at the closing brace of its class body.
You can make the code work by moving the
IdentityFdefinition out of the class body, to a point where the class template is complete:The problem is not related to initializer lists, it would apply to any constructor used at that point (not just a constructor taking an
initializer_list)