c – boost :: bind()是通过引用还是按值复制参数?

前端之家收集整理的这篇文章主要介绍了c – boost :: bind()是通过引用还是按值复制参数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么valgrind的DRD工具抱怨“线程加载冲突…大小为4”:关于这样的代码
void SomeFunction(const int& value)
{
    boost::bind(...,value); /* <-- complaines on this line
                                with last backtrace function "new(int)" */
}

boost :: bind()是否按引用或值存储值?

解决方法

按价值. 1

但你可以改为copy by 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

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);

猜你在找的C&C++相关文章