为什么这个片段使用统一的初始化编译g ++ 4.6而不是g ++ 4.7?

请注意, 派生使用C ++ 11统一初始化语法来调用基类构造函数。

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编译这个,但是g ++ 4.7没有:

 $ 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 

这是怎么回事?

更新1:它也编译没有警告与铿锵语 – 3.1
更新2: 看起来像一个编译器错误肯定。 这显然是固定在GCC 4.7.3。

GCC / libstdc ++贡献者Paolo Carlini 证实这是一个错误/回归 。

这可能是因为在版本4.7中添加了C11显式重写控件。

用icpc编译(英特尔编译器testing版本11.1 – > 12.1)给出:

 -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) 

编辑:但是,然后再次,c + + 11还没有完全实现icpc或http://software.intel.com/en-us/articles/c0x-features-supported-by-intel-c-compiler/

与g ++相同http://gcc.gnu.org/gcc-4.7/cxx0x_status.html

这明确指出它仍然是实验性的,所以一个bug很可能。

我find了这个:

草案说,初始化引用的初始化列表不是通过直接绑定完成的,而是通过首先在初始化列表中构造一个临时元素,然后将目标引用绑定到临时的“

因此,基于{}创build的临时文件是通过受保护的构造函数完成的,这可能会令人窒息。