【发布时间】:2017-05-10 12:13:15
【问题描述】:
鉴于此代码:
struct A {
A(int e) { throw e; }
};
struct B {
A a{42}; // Same with = 42; syntax
};
int main() {
try {
B b;
} catch (int const e) {
return e;
}
}
使用 GCC 编译时(版本 4.7.4、4.8.5、4.9.3、5.4.0、6.3.0):
$ g++ -std=c++11 test.cpp -o test; ./test ; echo $?
terminate called after throwing an instance of 'int'
Aborted
134
但是当使用 Clang(4.0.0 版)编译时:
$ clang++ -std=c++11 test.cpp -o test; ./test ; echo $?
42
哪种行为是正确的?
【问题讨论】:
-
Clang 是正确的。
-
啊,是的!可能与 GCC PR 80683 中的问题相同。
-
其他人最初将标题读为“这是一个好的设计模式”吗? ;)
标签: c++ c++11 exception exception-handling compiler-bug