次のコードがあります。
#include <iostream>
#include <vector>
#include <tr1/memory>
struct FooError {};
struct Foo
{
~Foo() { std::cerr << "~Foo() executed" << std::endl; }
explicit Foo(unsigned int index) { if (5 == index) throw FooError(index); };
};
int main() {
typedef std::tr1::shared_ptr<Foo> FooPtr;
std::vector<FooPtr> foos;
for (unsigned int index = 0; index < 20; ++index)
{
try
{
foos.push_back(FooPtr(new Foo(index)));
}
catch (const FooError&)
{
std::cerr << "FooError caught" << std::endl;
}
}
}
ブロック~Foo()
があるときに実行されるセットが表示されます。try{} catch{}
例外ハンドラがない場合、何も出力されません。例外が処理されるときに、スタック割り当てオブジェクトのデストラクタが呼び出されるということですか? または、std::cerr バッファリングの問題のために何も出力されませんか?