Why is this fragment using single initialization compiled with g ++ 4.6 but not g ++ 4.7?

Note that the derivative uses the C ++ 11 syntax to invoke the base class constructor.

class base { protected: base() {} }; class derived : public base { public: derived() : base{} // <-- Note the c++11 curly brace syntax // using uniform initialization. Change the // braces to () and it works. {} }; int main() { derived d1; return 0; } 

g ++ 4.6 compiles this, however g ++ 4.7 does not:

 $ g++-4.7 -std=c++11 -Wall -Wextra -pedantic curly.cpp -o curly curly.cpp: In constructor 'derived::derived()': curly.cpp:4:13: error: 'base::base()' is protected curly.cpp:19:24: error: within this context 

What's happening?

Update 1: it also compiles without warning using clang ++ - 3.1
Update 2: Looks like a compiler error. This seems to be fixed in GCC 4.7.3.

+46
c ++ gcc c ++ 11 g ++ uniform-initialization
Sep 07 '12 at 7:01
source share
4 answers

Paolo Carlini, developer of GCC / libstdc ++, has confirmed that this is a bug / regression .

+3
Oct 17 '12 at 22:52
source share

This is likely due to the fact that version 4.7 added explicit C11 override control.

-one
Sep 14 '12 at 17:18
source share

compilation using icpc (Intel compiler tested with version 11.1 → 12.1) gives:

 -bash-3.2$ icpc -std=c++0x test.c test.c(15): error: expected a declaration {} ^ test.c(12): error: expected a "(" : base{} // <-- Note the c++11 curly brace syntax ^ compilation aborted for test.c (code 2) 

edit: but then again, C ++ 11 is not yet fully implemented in icpc or http://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler/

same as with g ++ http://gcc.gnu.org/gcc-4.7/cxx0x_status.html

which clearly states that it is still experimental, so the error is very likely.

-one
Sep 18
source share

I found this:

"The project says that the list of initializers that initialize the link is not done by direct binding, but by first creating a temporary exit from the element in the list of initializers, and then linking the target link to this temporary one"

Thus, this can be overwhelmed by the fact that the temporary creation of the base {} is performed through a protected constructor.

-one
Sep 21 '12 at 16:47
source share



All Articles