6

std::bind を使用してメンバー関数を boost::signals2::signal::connect() に渡すのは安全ですか? つまり、boost::bind と std::bind は交換可能ですか?

VC++ 2010 SP1 でコンパイルできますが、テンプレート コードは私の頭をはるかに超えており、未定義の動作領域に足を踏み入れているのではないかと心配しています。

4

2 に答える 2

2

I'm not experienced in the subject by I would expect connect to take anything that implements a valid function call operator. It should be safe to call it with any function or function object that matches the signature, be it boost::bind, std::bind or anything else. Boost libraries are designed to be generic, so they usually don't go picking each others implementation details.

于 2011-10-10T07:24:48.523 に答える
1

このconnect関数はboost::functionオブジェクトを受け取ります。これは、基本的に、定義されているすべてのものを包む汎用ラッパーです。operator()したがって、拘束しているものとまったく同じくらい安全です。

たとえば、これはかなり安全です。

boost::shared_ptr<ClassName> pValue = boost::make_shared<ClassName>(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

boost::shared_ptrデータの一部として を格納するため、これはかなり安全です。

ClassName *pValue = new ClassName(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

これは条件付きで安全です。その接続がまだ存在しているときに を実行すると、即座に安全ではなくなりますdelete pValue

個人的には、「条件付きで安全」をあまり信じていませんが、それはあなた次第です。ポイントは、バインド先のすべてが、バインドされてboost::bindいる限り存在し続けなければならないということです。

于 2011-10-10T07:50:37.320 に答える