c++ - Why a C-style typecasting is mandatory while initializing a POD data in initializer list? -
struct pod { int i, j; }; class { 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 in old production code compiled g++34
, until din't know feature.
g++ specific feature ? if not then, why typecasting needed , that's c-style cast allowed ?
the syntax you're using isn't initializer lists, it's initialization of class types outside of declarations. example:
pod p; p = (pod) {1, 2};
these called compound literals; added c in c99. aren't supported in c++; gcc allows them in c++ (and c89) extension. c++11 adds syntax:
p = pod({1, 2});
or in case:
a() : m_pod(pod({1,2})) {}
Comments
Post a Comment