オブジェクトの初期化がエラーを出力する場合

コンストラクタが例外を投げるのではなく、factoryがエラーを返すのがいい。

#include <iostream>
#include <boost/optional.hpp>

struct X {
    bool initialize() { return true; }
};

boost::optional<X> factoryX()
{
    X x;
    if (x.initialize()) {
        return x;
    }
    return boost::none;
}

int main()
{
   boost::optional<X> x = factoryX();
}

factoryは、コンストラクタ(初期化処理)の仮想関数化にも使える。

参照