The following code is pretty trivial, and I expected it to compile.
struct A { struct B { int i = 0; }; B b; A(const B& _b = B()) : b(_b) {} };
I tested this code with g ++ versions 4.7.2, 4.8.1, clang ++ 3.2 and 3.3. Besides the fact that g ++ 4.7.2 segfaults to this code ( http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57770 ), other proven compilers provide error messages that do not explain much.
g ++ 4.8.1:
test.cpp: In constructor 'constexpr A::B::B()': test.cpp:3:12: error: constructor required before non-static data member for 'A::B::i' has been parsed struct B ^ test.cpp: At global scope: test.cpp:11:23: note: synthesized method 'constexpr A::B::B()' first required here A(const B& _b = B()) ^
clang ++ 3.2 and 3.3:
test.cpp:11:21: error: defaulted default constructor of 'B' cannot be used by non-static data member initializer which appears before end of class definition A(const B& _b = B()) ^
The ability to compile this code is possible and does not seem to matter. There are two options:
struct B { int i = 0; B(){}
or
struct B { int i; B() : i(0) {}
Is this code really wrong or are the compilers wrong?
c ++ language-lawyer c ++ 11
etam1024 Jul 02 '13 at 16:03 2013-07-02 16:03
source share