不能使用try/catch在构造函数初始化列表中使用统一初始化

cat*_*dle 10 c++ gcc g++ uniform-initialization c++11

下面的代码不会用gcc编译:

struct test {
    int x;
    test() try : x{123} {
    }
    catch (...) {
    }
};

int main() {}
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

更改x{123}x(123)帮助.这应该(不)以这种方式工作吗?

aki*_*kim 1

根据标准的语法,这是有效的(请参阅 [gram.special] 的大括号,以及 [gram. except] 的try- catch。GCC 4.8 有错误,但 GCC 4.9 可以正确处理它(就像其他编译器一样,已经报道)。

我不知道为什么 BS 在他的书中没有使用这种语法。也许是因为当他编译示例以查看它们是否正确(如果正确)时,他手头没有任何支持此语法的编译器?