我是靠谱客的博主 娇气方盒,这篇文章主要介绍std::function用法详解std::function用法详解,现在分享给大家,希望可以做个参考。

std::function用法详解

代码在:VCCommon/functionDemo

std::function 简介

类模板 std :: function 是一个通用的多态函数包装器。 std :: function 的实例可以存储,复制和调用任何可调用的目标 :包括函数,lambda表达式,绑定表达式或其他函数对象,以及指向成员函数和指向数据成员的指针。当std::function对象未包裹任何实际的可调用元素,调用该 std::function 对象将抛出std::bad_function_call 异常。

与函数指针的比较

#include<iostream>
#include <functional>

using namespace std;

// c type global function
int c_func(int a, int b)
{
    return a + b;
}

int main()
{
    typedef int(*Func)(int ,int);
    Func f1 = c_func;
	cout<< f1(1,2)<<endl;  //3
   
	std::function<int(int, int)>f2 = c_func;
	cout<<f2(1, 2)<<endl;      // 3
    system("pause");
	return 0;
}

从上面我们可以看出,使用C++11的function类调用函数方便多了。

function 类模板

template< class R, class... Args >
class function<R(Args...)>;

模板参数说明:

  • R: 被调用函数的返回类型
  • Args…:被调用函数的形参

例如:function<int(int,int)> func;
则 function 类的实例 func 可以指向返回值为int型,有两个形参都为int型的函数。

function 的成员函数

虽然是function是类模板,但其只有成员函数,无数据成员。

成员函数声明说明
constructor构造函数:constructs a new std::function instance
destructor析构函数: destroys a std::function instance
operator=给定义的function对象赋值
operator bool检查定义的function对象是否包含一个有效的对象
operator()调用一个对象

用法

1.调用普通函数

非模板

#include <functional>
#include <iostream>

int f(int a, int b)
{
    return a+b;
}

int main()
{
    std::function<int(int, int)>func = f;
    cout<<func(1, 2)<<endl;      // 3
    
    system("pause");
    return 0;
}

有模板

#include <functional>
#include <iostream>

template<class T>
T f(T a, T b)
{
    return a+b;
}

int main()
{
    std::function<int(int, int)>func = f<int>;
    cout<<func(1, 2)<<endl;      // 3
    
    system("pause");
    return 0;
}

2.调用函数对象

非模板

#include<iostream>
#include <functional>

using namespace std;

//function object
struct functor  // or class functor
{
public:
    int operator() (int a, int b)
    {
        return a + b;
    }
};

int main()
{
    functor ft;
    function<int(int,int)> func = ft();
    cout<<func(1,2)<<endl;    //3
    
    system("pause");
    return 0;
}

有模板

#include<iostream>
#include <functional>

using namespace std;

//function object
template<class T>
struct functor   // or class functor
{
public:
    T operator() (T a, T b)
    {
        return a + b;
    }
};

int main()
{
    functor<int> ft;
    function<int(int,int)> func = ft;
    //function<int(int,int)> func = functor<int>();
    cout<<func(1,2)<<endl;    //3
    
    system("pause");
    return 0;
}

3.调用lambda表达式

#include <functional>
#include <iostream>

using namespace std;

int main()
{
	auto f = [](const int a, const int b) { return a + b; };
	std::function<int(int, int)>func = f;
	cout << func(1, 2) << endl;      // 3
	system("pause");
	return 0;
}

4.调用类静态成员函数

非模板

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    static int plus(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    function<int(int, int)> f = Plus::plus;
    //function<int(int, int)> f = &Plus::plus;
    cout << f(1, 2) << endl;     //3
    
    system("pause");                                       
    return 0;
}

有模板

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    template <class T>
    static T plus(T a, T b)
    {
        return a + b;
    }
};

int main()
{
    function<int(int, int)> f = Plus::plus<int>;
    //function<int(int, int)> f = &Plus::plus<int>;
    cout << f(1, 2) << endl;     //3
    
    system("pause");                                       
    return 0;
}

5.调用类成员函数

非模板

#include <iostream>
#include <functional>

using namespace std;

class Plus
{
public:
    int plus(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    Plus p;
    function<int(Plus&,int, int)> f = &Plus::plus;
    function<int(Plus,int, int)> f2 = &Plus::plus;
    cout << f(p,1, 2) << endl;     //3
    cout << f2(p,1, 2) << endl;     //3
    
    system("pause");                                       
    return 0;
}

有模板

#include <iostream>
#include <functional>

using namespace std;

class Plus
{
public:
    template <class T>
    T plus(T a, T b)
    {
        return a + b;
    }
};

int main()
{
    Plus p;
    function<int(Plus&,int, int)> f = &Plus::plus<int>;
    function<int(Plus,int, int)> f2 = &Plus::plus<int>;
    cout << f(p,1, 2) << endl;      //3
    cout << f2(p,1, 2) << endl;     //3
    
    system("pause");                                       
    return 0;
}

6.调用类公有数据成员

非模板

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    Plus(int num_):num(num_){}
    
    int  num;
};

int main()
{
    Plus p(2);
    function<int(Plus&)> f = &Plus::num;
    function<int(Plus)> f2 = &Plus::num;
    cout << f(p) << endl;     //2
    cout << f2(p) << endl; 
    
    system("pause");                                       
    return 0;
}
#include <iostream>
#include <functional>

using namespace std;

template <class T>
class Plus
{
public:
    Plus(T num_):num(num_){}
    
    T  num;
};

int main()
{
    Plus<int> p(2);
    function<int(Plus<int>&)> f = &Plus<int>::num;
    function<int(Plus<int>)> f2 = &Plus<int>::num;
    cout << f(p) << endl;      //2
    cout << f2(p) << endl;     //2
    
    system("pause");                                       
    return 0;
}

7.通过bind函数调用类成员函数

非模板

#include <iostream>
#include <functional>
using namespace std;

class Plus
{
public:
    int plus(int a, int b)
    {
        return a + b;
    }
};

class Plus2
{
public:
    static int plus(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    Plus p;
    // 指针形式调用成员函数
    function<int(int, int)> f1 = bind(&Plus::plus, &p, placeholders::_1, placeholders::_2);// placeholders::_1是占位符
    
    // 对象形式调用成员函数
    function<int(int, int)> f2 = bind(&Plus::plus, p, placeholders::_1, placeholders::_2);// placeholders::_1是占位符
    cout << f1(1, 2) << endl;     //3
    cout << f2(1, 2) << endl;     //3
    
    Plus2 p2;
	// 指针形式调用成员函数
	function<int(int, int)> f3 = bind(Plus2::plus, placeholders::_1, placeholders::_2);// placeholders::_1是占位符
	cout << f3(1, 2) << endl;     //3
    
    system("pause");                                       
    return 0;
}

有模板

#include <iostream>
#include <functional>

using namespace std;

class Math
{
public:
    template <class T>
    T Minus(T i, T j)
    {
        return i - j;
    }
};

int main()
{
	Math m;
    function<int(int, int)> f = bind(&Math::Minus<int>, &m, placeholders::_1, placeholders::_2);
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}

最后附上一段代码:

#include <iostream>
#include <map>
#include <functional>

using namespace std;
 
// 普通函数
int add(int i, int j) { return i + j; }

// lambda表达式
auto mod = [](int i, int j){return i % j; };

// 函数对象类
struct divide
{
	int operator() (int denominator, int divisor)
	{
		return denominator / divisor;
	}
};

int main()
{
	map<char, function<int(int, int)>> binops = 
	{
		{ '+', add },
		{ '-', [](int i, int j){return i - j; } },
		{ '/', divide() }
	};
	cout << binops['+'](10, 5) << endl;
	cout << binops['-'](10, 5) << endl;
	cout << binops['/'](10, 5) << endl;
    
	system("pause");
	return 0;
}

该文章持续更新,欢迎大家批评指正。

推荐一个零声学院免费公开课程,个人觉得老师讲得不错,
分享给大家:[Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容,点击立即学习:
服务器课程:C++服务器

最后

以上就是娇气方盒最近收集整理的关于std::function用法详解std::function用法详解的全部内容,更多相关std内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部