boost :: bind()是按引用还是按值复制参数? [英] Does boost::bind() copy parameters by reference or by value?
本文介绍了boost :: bind()是按引用还是按值复制参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么valgrind的DRD工具会抱怨线程加载冲突...大小为4:关于这样的代码:
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)" */
}
boost :: bind()是否按引用或值存储值?
Does boost::bind() stores values by reference or value?
推荐答案
值。 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;
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, ref(i), _1);
bind(f,cref(42),_1);
bind(f, cref(42), _1);
这篇关于boost :: bind()是按引用还是按值复制参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文