6

valgrind の DRD ツールが「サイズ 4 でのスレッドによる負荷の競合」と不平を言うのはなぜですか: そのようなコードについて:

void SomeFunction(const int& value)
{
    boost::bind(..., value); /* <-- complaines on this line
                                with last backtrace function "new(int)" */
}

boost::bind() は参照または値によって値を保存しますか?

4

1 に答える 1

14

値による。1

ただし、代わりにref でコピーすることができます。

void SomeFunction(const int& value)
{
    boost::bind(..., boost::ref(value)); 
    boost::bind(..., boost::cref(value)); // by const ref
}

1 http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#目的

i の値のコピーが関数オブジェクトに格納されます。boost::ref と boost::cref を使用して、関数オブジェクトにコピーではなくオブジェクトへの参照を格納させることができます: int i = 5;

bind(f, ref(i), _1);

bind(f, cref(42), _1);

于 2011-06-18T15:46:28.503 に答える