struct POD { int i, j; };
class A {
POD m_pod;
public:
A() : m_pod({1,2}) {} // error
A() : m_pod(static_cast<POD>({1,2})) {} // error
A() : m_pod((POD) {1,2}) {} // ok!
};
I see this in an old production code compiled with g++34, until then I din’t know this feature.
Is it a g++ specific feature ? If not then, why is typecasting needed and that’s too only C-style cast is allowed ?
The syntax you’re using isn’t just for initializer lists, it’s for any initialization of class types outside of their declarations. For example:
These are called compound literals; they were added to C in C99. They aren’t actually supported in C++; GCC allows them in C++ (and C89) as an extension. C++11 adds the syntax:
Or in your case: