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#Purpose

i 的值的副本存储到函数对象中。boost::ref 和 boost::cref 可用于使函数对象存储对对象的引用,而不是副本: int i = 5;

绑定(f,参考(i),_1);

绑定(f,cref(42),_1);

于 2011-06-18T15:46:28.503 回答