Why is C type typing required when initializing POD data in the initializer list?

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 the old production code compiled with g++34, until then I do not know this function.
Is this a g ++ feature? If not, then why typecasting is required and what is allowed only C-style?

+5
source share
2 answers

The syntax you use is intended not only for lists of initializers, but also for any initialization of class types outside their declarations. For instance:

POD p;
p = (POD) {1, 2};

; C C99. ++ ; GCC ++ ( C89) . ++ 11 :

p = POD({1, 2});

:

A() : m_pod(POD({1,2})) {}
+4

++ ( ++ 03, ++ 11):

A() : m_pod((POD) {1,2}) {} // ok!

GCC , GCC.

-pedantic, :

pod.cpp: 8: 29: : ISO ++


++ 11 :

A() : m_pod{1,2} {}

: http://ideone.com/XaO4y

:

class A {
  POD m_pod {1,2}; //in-place initialization
public:
  A() {}
};

Ideone , .

+6

All Articles