Does boost::bind() copy parameters by reference or by value?

强颜欢笑 提交于 2019-12-17 16:37:39

问题


Why does valgrind's DRD tool complaines "Conflicting load by thread ... at size 4": about such code:

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

Does boost::bind() stores values by reference or value?


回答1:


By value. 1

But you can make it copy by ref instead:

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

a copy of the value of i is stored into the function object. boost::ref and boost::cref can be used to make the function object store a reference to an object, rather than a copy: int i = 5;

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

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



来源:https://stackoverflow.com/questions/6397092/does-boostbind-copy-parameters-by-reference-or-by-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!