following code不使用 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: 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
-catch
见 [gram.except]。GCC 4.8 有错误,但 GCC 4.9 正确处理了它(正如其他编译器一样,如前所述)。
我不知道为什么 BS 在他的书中不使用这种语法。也许是因为当他编译他的示例以查看它们是否正确(如果他这样做了)时,他手头没有任何支持这种语法的编译器?
关于c++ - 不能在带有 try/catch 的构造函数初始化列表中使用统一初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18675881/