boost库function与bind

boost库function与bind


一、function
头文件:boost/function.hpp
function更合适的说法我觉得是一种回调函数的表现方式。

boost::function是一个函数对象的“容器”,概念上像是C/C++中函数指针类型的泛化,是一种“智能函数指针”。它以对象的形式封装了原始的函数指针或函数对象,能够容纳任意符合函数签名的可调用对象。在声明 functions 时,声明中最重要的部分是函数的签名。这部分即是告诉 function 它将保存的函数或函数对象的签名和返回类型。

boost::function能够代替函数指针,并且能能接受函数或函数对象,增加了程序的灵活性。但是boost::function相比函数指针来说体积稍大一点,速度上稍慢一点。


函数原型:boost::function<float (int x, int y)> f;

看一个简单的例子:

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>

typedef boost::function<int(int,char)>Func;

int test(int num,char ch)
{
	std::cout<<num<<" "<<ch<<std::endl;
}

int main()
{
	Func f;
	f = &test;
	f(1,'a');
	return 0;
}

//回调函数

结果是:

localhost@localhost:/mnt/hgfs/share/boost$ ./function
1 a

二、bind
头文件:boost/bind.hpp

bind的基本形式如下:

template<class R,class F> bind(F f);
template<class R,class F,class A1> bind(F f,A1 a1);
namespace
{
boost::arg<1> _1;
boost::arg<2> _2;
boost::arg<3> _3;
…..                                     //其他6个占位符
};

bind接收的第一个参数必须是一个可调用的对象f,包括函数函数指针函数对象、和成员函数指针,之后bind最多接受9个参数参数数量必须与f的参数数量相等,这些参数被传递给f作为入参。 绑定完成后,bind返回一个函数对象,它内部保存了f的拷贝,具有operator()返回值类型自动推导f的返回类型。在发生调用时这个函数对象将把之前存储的参数转发给f完成调用。例如,有一个函数func,它的形式是:

1 func(a1,a2);

那么,他将等价于一个具有无参operator()的bind函数对象调用:

1 bind(func,a1,a2)();

这是bind最简单的形式,bind表达式存储了func和a1、a2的拷贝,产生了一个临时函数对象。因为func接收两个参数,而a1和a2的拷贝传递给func完成真正的函数调用。

bind的真正威力在于它的占位符,它们分别定义为**_1,_2,_3**,一直到 _9,位于一个匿名的名字空间。占位符可以取代bind参数的位置,在发生调用时接受真正的参数占位符的名字表示它在调用式中的顺序,而在绑定的表达式中没有没有顺序的要求,_1不一定必须第一个出现,也不一定只出现一次,例如:

1 bind(func,_2,_1)(a1,a2);

返回一个具有两个参数的函数对象,第一个参数将放在func的第二个位置,而第二个参数则放在第一个位置,调用时等价于:

1 func(a2,a1);

看一下例子:

#include <functional>
#include <iostream>
#include <string>
#include "boost/bind.hpp"

void print_string(const std::string s) 
{  std::cout << s << '\n';
}
void print_functionname()
{
    std::cout << "Print_functionname" <<std::endl;
}

int main()
{
	boost::bind(print_functionname)();
	return 0;
}

结果:

localhost@localhost:/mnt/hgfs/share/boost$ ./bind
Print_functionname

包含类的例子:

#include <functional>
#include <iostream>
#include <string>
#include "boost/bind.hpp"
class Test 
{
public:      
    void print_string(const std::string& s) const
    {    
        std::cout << s << '\n'; 
    }
    void print()
    {
        std::cout << "Test" << std::endl;
    }
};

int main()
{
	Test ts;

	boost::bind(&Test::print,_1)(ts);
	boost::bind(&Test::print_string,_1,_2)(ts,"hello Test");
	return 0;
}

结果:

localhost@localhost:/mnt/hgfs/share/boost$ ./bind
Test
hello Test


三、function与bind联合使用
一般bind和function会在一起使用,这里只写一个简单的例子

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

typedef boost::function<void(void)>Func;

void print_string(const std::string s)
{
    std::cout<<"s:"<<s<<std::endl;
}

int main()
{
	Func f(boost::bind(print_string,"hello bind"));
	f();
    return 0;
}

结果:

localhost@localhost:/mnt/hgfs/share/boost$ ./bind1
s:hello bind


参考文章:https://www.cnblogs.com/blueoverflow/p/4740093.html


想了解学习更多C++后台服务器方面的知识,请关注:
微信公众号:C++后台服务器开发


发布了197 篇原创文章 · 获赞 68 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/Travelerwz/article/details/99696406