C++ can';t使用try/catch在构造函数初始化列表中使用统一初始化
不使用gcc编译:C++ can';t使用try/catch在构造函数初始化列表中使用统一初始化,c++,gcc,c++11,g++,uniform-initialization,C++,Gcc,C++11,G++,Uniform Initialization,不使用gcc编译: struct test { int x; test() try : x{123} { } catch (...) { } }; int main() {} 错误: prog.cpp:3:25: error: expected unqualified-id before ‘{’ token test() try : x{123} { ^ prog.cpp:5:5: error
struct test {
int x;
test() try : x{123} {
}
catch (...) {
}
};
int main() {}
错误:
prog.cpp:3:25: error: expected unqualified-id before ‘{’ token
test() try : x{123} {
^
prog.cpp:5:5: error: expected unqualified-id before ‘catch’
catch (...) {
^
prog.cpp: In constructor ‘test::test()’:
prog.cpp:3:23: error: expected ‘{’ at end of input
test() try : x{123} {
^
prog.cpp:3:23: error: expected ‘catch’ at end of input
prog.cpp:3:23: error: expected ‘(’ at end of input
prog.cpp:3:23: error: expected type-specifier at end of input
prog.cpp:3:23: error: expected ‘)’ at end of input
prog.cpp:3:23: error: expected ‘{’ at end of input
将
x{123}
更改为x(123)
会有所帮助。这是否应该(不)以这种方式工作?根据标准的语法,这是有效的(大括号参见[gram.special],而try参见[gram.except]
-catch
。GCC4.8有错误,但GCC4.9处理正确(正如已经报告的其他编译器一样)
我不知道为什么BS在他的书中没有使用这种语法。可能是因为他手头没有任何编译器在编译示例时支持这种语法,以查看它们是否正确(如果他有)“< /p> C++没有任何与Brand和C++ 11有关的错误,很可能在编译器中有一段时间没有完全实现角点……@ KerrekSB我最初认为这是一个bug,但是在Stroustrup的书中,他使用了构造函数中的统一初始化,除了有一个TIG/catch,所以有点让我困惑。英特尔C++131.3COMPI也可以无错误地编译它。gcc主线编译它。