boost::bind()是否通过引用或值复制参数
Does boost::bind() copy parameters by reference or by value?
为什么valgrind的DRD工具抱怨"线程负载冲突…大小为4":关于这样的代码:
void SomeFunction(const int& value)
{
boost::bind(..., value); /* <-- complaines on this line
with last backtrace function "new(int)" */
}
boost::bind()是否通过引用或值存储值?
按值。1
但你可以通过引用复制它:
void SomeFunction(const int& value)
{
boost::bind(..., boost::ref(value));
boost::bind(..., boost::cref(value)); // by const ref
}
1http://www.boost.org/doc/libs/1_46_1/libs/bind/bind.html#Purpose
i的值的副本被存储到函数对象中。boost::ref和boost::cref可用于使函数对象存储对对象的引用,而不是副本:int i=5;
bind(f,ref(i),_1);
bind(f,cref(42),_1);
相关文章:
- 深层复制具有自引用指针的类
- 对复制 CTOR 和 CTOR 的未定义引用
- 为什么我的运算符 + 重载尽管是通过引用传递的,但仍调用我的复制构造函数?
- 在引用初始化中使用已删除的复制构造函数进行复制初始化
- 关于隐式声明的复制构造函数的引用在逻辑上不清楚
- C++:右值引用构造函数和复制省略
- 通过基类接受方法转发派生 UniquePtr 的右值会移动引用而不是复制
- 如何复制只引用基类的派生类
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- C++为具有引用成员变量的类创建复制构造函数
- std::vector 是否有用于引用的复制构造函数?
- 是复制了新数组还是指向结构中引用的数组的指针?
- 可移动但不可复制的对象:按值传递还是按引用传递?
- 通过引用传递是否涉及复制地址
- 复制引用结构上的赋值
- C++初始化指针/引用/复制细微差别
- 如何在C++中通过引用复制对象
- 如何将一个向量的元素引用复制到另一个向量?
- C++对象构造函数通过常量引用复制传递
- 通过引用复制本地对象