我有以下代码:
#include <iostream>
using namespace std;
int foo()
{
throw 1;
}
struct A {
int a;
public:
A() try : a(foo())
{
cout << "Constructor A\n";
} catch(...)
{
cout << "Catched in A\n";
}
};
struct B : A {
B()
{
cout << "Constructor B\n";
::foo();
} catch(...)
{
cout << "Catched in B\n";
}
void foo()
{
}
catch(...)
{
cout << "Catched in foo\n";
}
};
int main () try
{
B b;
return 0;
} catch (...)
{
cout << "Catched in main\n";
}
输出:
抓住A
抓住主要
为什么B的阻挡块没有捕获任何东西?如果没有尝试它怎么能存在?
Clang只检测第26行的错误:
error: expected member name or ';' after declaration specifiers
并认为B::foo
的阻止是正常的。我很困惑:)
Clang ++ v 3.4
gcc v 4.8