std::function & Functor(用 Functor 构造std::function时的注意点)

std::function 可以保存一切可调用的对象( Functor, lamda,bind对象)

std::funciton 可以复制该对象然后绑定复制后的对象的operator()——传值,也可以直接绑定该对象的operator() ——传引用。
std::bind 传参数也是默认复制该对象,如果希望传引用使用 std::refstd::bind uses value semantics by default, but you can use std::ref

Functor:

class Functor
{
public:
	Functor()=default;  // 注意, 在 c++11中,如果声明了其中一个特殊函数(如,复制构造),则编译器不会默认生成另外的特殊函数(如这个构造函数),需要显示声明!
	int operator() (int a, int b)
	{
		return a+b;
	}
private:
	Functor(const Functor&);  // 不能复制,for test
}

std::function

#include <functional>
#include <memory>
#include <iostream>

int main()
{
	Functor f;
	
	// 这里会调用的是 Functor 的复制构造函数,如果不能复制则不能编译通过。
	// 调用 a 将调用复制构造后的新对象的 operator()
	std::function<int(int, int)> a = f;
	
	// 如果 Functor不能够被复制,或者Functor中有成员变量不能够被复制(即默认复制构造失败),则不能够直接用Functor对象来构造 std::function 对象
	// 不过下面这样是可以的(不会触发Functor任何构造函数), 只不过要确保在b的生存周期内f一直有效。
	// 调用 b 将调用原对象 f 的 operator()
	std::function<int(int, int)> b = std::ref(f);

    // 使用 bind 和 传地址
    std::function<int(int, int)> c = std::bind( &Functor ::operator(), &f );
	
	// 另外,如果Functor提供了 move 构造函数,则下面会调用Functor 的 move 构造函数, 如果不能被move则会被copy构造(因此,复制构造函数也必须显示写出,否则编译不通过)
	std::function<int(int, int)> d = Functor();
	
	// 这样可以,只会调用Functor的构造函数,不会触发复制构造或move构造, good!
	std::function<int(int, int)> e = std::bind(&Functor::operator(), std::make_shared<Functor>(), std::placeholders::_1, std::placeholders::_2);

	return 0;
}

因此,在将 Functor 保存到 std::function的时候要考虑类成员的可复制性( 和 move),或者使用最后的make_shared+bind的方法,否则会编译不通过问题。

see link:
http://blog.csdn.net/GW569453350game/article/details/46649517
https://gcc.gnu.org/onlinedocs/libstdc++/libstdc+±api-4.6/a00481.html
http://stackoverflow.com/questions/27953336/constructing-stdfunction-target-in-place

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值