class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any Callable target -- functions,lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.

There are two performance implications of using std::function that might surprise you:

(1)、When calling a std::function, it does a virtual function call.

(2)、When assigning a lambda with significant captures to a std::function, it will do a dynamic memory allocation.

类模版std::function是一种通用、多态的函数封装。std::function的实例可以对任何可以调用的目标实体进行存储、复制、和调用操作,这些目标实体包括普通函数、Lambda表达式、函数指针、以及其它函数对象等。

通过std::function对C++中各种可调用实体(普通函数、Lambda表达式、函数指针、以及其它函数对象等)的封装,形成一个新的可调用的std::function对象,让我们不再纠结那么多的可调用实体。

std::function实现了一套类型消除机制,可以统一处理不同的函数对象类型。以前使用函数指针来完成这些,现在可以使用更安全的std::function来完成这些任务。

C++11中推出std::function是为了泛化函数对象,函数指针,引用函数,成员函数的指针,让我们可以按更统一的方式写出更加泛化的代码。

std::function对象是对C++中现有的可调用实体的一种类型安全的包裹(像函数指针这类可调用实体,是类型不安全的)。

可调用实体转换为std::function对象需要遵守以下两条原则:

(1)、std::function对象的参数能转换为可调用实体的参数;

(2)、可调用实体的返回值能转换为std::function对象的返回值(注意,所有的可调用实体的返回值都与返回void的std::function对象的返回值兼容)。

在C++中,”可调用对象”概念:(1)、是一个函数指针;(2)、是一个具有operator()成员函数的类对象(仿函数);(3)、是一个可被转换为函数指针的类对象;(4)、是一个类成员(函数)指针。

下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:

#include "function.hpp"
#include <iostream>
#include <string>
#include <functional>
#include <vector>

///////////////////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/utility/functional/function
struct Foo {
	Foo(int num) : num_(num) {}
	void print_add(int i) const { std::cout << num_ + i << '\n'; }
	int num_;
};

void print_num(int i)
{
	std::cout << i << '\n';
}

struct PrintNum {
	void operator()(int i) const
	{
		std::cout << i << '\n';
	}
};

int test_function1()
{
	// store a free function
	std::function<void(int)> f_display = print_num;
	f_display(-9);

	// store a lambda
	std::function<void()> f_display_42 = []() { print_num(42); };
	f_display_42();

	// store the result of a call to std::bind
	std::function<void()> f_display_31337 = std::bind(print_num, 31337);
	f_display_31337();

	// store a call to a member function
	//std::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
	const Foo foo(314159);
	//f_add_display(foo, 1);

	// store a call to a data member accessor
	//std::function<int(Foo const&)> f_num = &Foo::num_;
	//std::cout << "num_: " << f_num(foo) << '\n';

	// store a call to a member function and object
	using std::placeholders::_1;
	std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1);
	f_add_display2(2);

	// store a call to a member function and object ptr
	std::function<void(int)> f_add_display3 = std::bind(&Foo::print_add, &foo, _1);
	f_add_display3(3);

	// store a call to a function object
	std::function<void(int)> f_display_obj = PrintNum();
	f_display_obj(18);

	return 0;
}

///////////////////////////////////////////////////////////
// reference: https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/
void execute(const std::vector<std::function<void()>>& fs)
{
	for (auto& f : fs)
		f();
}

void plain_old_func()
{
	std::cout << "I'm an old plain function" << std::endl;
}

class functor
{
public:
	void operator()() const
	{
		std::cout << "I'm a functor" << std::endl;
	}
};

int test_function2()
{
	std::vector<std::function<void()>> x;
	x.push_back(plain_old_func);

	functor functor_instance;
	x.push_back(functor_instance);
	x.push_back([]()
	{
		std::cout << "HI, I'm a lambda expression" << std::endl;
	});

	execute(x);

	return 0;
}

///////////////////////////////////////////////////////
// reference: http://shaharmike.com/cpp/lambdas-and-functions/
void global_f() {
	std::cout << "global_f()" << std::endl;
}

struct Functor {
	void operator()() { std::cout << "Functor" << std::endl; }
};

int test_function3()
{
	std::function<void()> f;
	std::cout << "sizeof(f) == " << sizeof(f) << std::endl;

	f = global_f;
	f();

	f = [](){ std::cout << "Lambda" << std::endl; };
	f();

	Functor functor;
	f = functor;
	f();

	return 0;
}

GitHubhttps://github.com/fengbingchun/Messy_Test

C++11中std::function的使用的更多相关文章

  1. C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  2. C++11 中的function和bind、lambda用法

    std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo ...

  3. C++ 中std::function 、std::bind的使用和lambda的使用

    std::function是可调用对象的包装器:std::bind是将可点用对象和其参数一起进行绑定,且绑定后的结果可以使用std::function对象进行保存,并延迟调用到需要调用的时候: 在C+ ...

  4. 转 C++11之std::function和std::bind

    std::function是可调用对象的包装器,它最重要的功能是实现延时调用: #include "stdafx.h" #include<iostream>// std ...

  5. C++11中std::bind的使用

    std::bind: Each argument may either be bound to a value or be a placeholder: (1).If bound to a value ...

  6. 【浅析C++11】std::function和std::bind

    目录 std::function可调用对象包装器 std::function基本用法 std::function/std::bind与抽象工厂.工厂方法的一点思考 std::function可调用对象 ...

  7. C++11中std::move的使用

    std::move is used to indicate that an object t may be "moved from", i.e. allowing the effi ...

  8. C++11中std::forward的使用 (转)

    std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...

  9. C++11中std::unordered_map的使用

    unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...

随机推荐

  1. python、数据分析师、算法工程师的学习计划

    1.前言 最近(2018.4.1)在百忙之中开通了博客,希望能够把自己所学所想沉淀下来,这篇是我开始系统学习python,成为数据分析师和算法工程师之路的计划,望有志于为同样目标奋斗的数据猿一起交流和 ...

  2. VMware,win7与linux centos6.4文件互传,linux下挂载windows共享文件夹,vmware tools安装方法

    本方法是以win7,VMware9.0.1 ,centos6.4为基础实验的. 对于linux的初级使用阶段,都会Windows中使用linux虚拟机VMWare或者其它的.在Windows与linu ...

  3. Spark Streamming 基本输入流I(-) :File/Hdfs

    Spark Streamming 基本输入流I(-):从文件中进行读取 文件读取1:本地文件读取 这里我只给出实现代码及操作步骤 1.在本地目录下创建目录,这里我们创建目录为~/log/ 2.然后手动 ...

  4. cf 786 B 线段树优化建图

    cf 786 B 链接 CF 思路 n个点,3种建边方式,规模\(O(n^2)\) 线段树优化建图 注意 读入的数据好坑啊,说好的v,u变成了u,v. 两棵树,一棵出,一棵入.线段树的作用只不过是按照 ...

  5. 【转】Android listview与adapter用法

    一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...

  6. 应用性能指数(APDEX)是如何计算出来的?

    应用性能指数(APDEX)是如何计算出来的?在应用性能管理领域聚合指标是一种常见手段,主要是用来把成百上千的指标通过某种计算方法聚合成一个或几个指标,用来反映应用的整体健康状态.在这些聚合指标中,比较 ...

  7. prior_box层

    https://www.jianshu.com/p/5195165bbd06 1.step_w.step_h其实就相当于faster中的feat_stride,也就是把这些点从feature map映 ...

  8. 学大伟业 Day 1 培训总结

    第一天培训,讲的基本算法,东西很多.还有些数论,图论,数据结构and some small tricks 一.输入输出技巧 //输入输出技巧 /* scanf.printf:速度快,需要记忆不同数据类 ...

  9. JS JavaScript闭包和作用域

    JavaScript高级程序设计中对闭包的定义:闭包是指有权访问另外一个函数作用域中变量的函数. 从概念上,闭包有两个特点: 1.函数 2.能访问另外一个函数的作用域中的变量 在ES6之前,JavaS ...

  10. Python 学习笔记(七)Python字符串(二)

    索引和切片 索引  是从0开始计数:当索引值为负数时,表示从最后一个元素(从右到左)开始计数 切片 用于截取某个范围内的元素,通过:来指定起始区间(左闭右开区间,包含左侧索引值对应的元素,但不包含右测 ...