The Boost function library contains a family of class template the are function object wrappers.The notion is similar to a generalized callback .It shares features with function pointers in that both define a call interface through which some implementation can be called and the implementation that is invoked may change throughout the course of the program .this actually helps to play with the functions as till now we just implemented the logics with the variables and now we are going to implement it with the functions.Boost function has two syntactical form :
1.the preferred syntax: the preferred form fits more closely with the c++ language and reduces the separate number of template parameters that
need to be considered,that also improves the readability.however it is not fit for all the compilers because of the compiler bugs .
eg: boost::function(int <int,int>) f;
2.the portable syntax: it is an compatible form which works on all the compilers
eg: boost::function2<int , int , int> f;
Header files:
there are two header files in the boost::function which are
#include<boost/function.hpp>
#include<boost/function_equal.hpp>
both the header files will be explained in more details in the next article . below one is the basic usage of it.
classes:
class bad_function_call: an exception type is thrown when an instance of a function object is empty when invoked
class function_base: the base class for all boost function objects.object of type function_base may not be created directly.
class functionN: A set of generalized function pointers that can be used for callbacks and wrapping function objects.
Syntax:
these are syntax of the functions are been defined in the boost::function:
1.//if resulttype is not void
Resulttype foo(Arg1 arg1,Arg2 arg2,....,AgrN agrN)
{ return f(arg1,arg2,....argN);
}
2.//if resulttype is void
Resulttype foo(Arg1 arg1,Arg2 arg2,....,AgrN agrN)
{ f(arg1,arg2,....argN);
}
A special provision is made for pointers to member functions. Though they are not function objects, Boost.Function will adapt them internally to function objects. This requires that a pointer to member function of the form R (X::*mf)(Arg1, Arg2, ..., ArgN) cv-quals
be adapted to a function object with the following function call operator overloads:
3. template<typename P>
R operator(cv -quals p& x,Arg1 arg1,Arg2 arg2,....,AgrN agrN) const
{
return (*x).*mf(arg1,arg2,....argN);
}
Program:
#include<boost/function.hpp> // this the necessary header file which must be included
#include <functional>
#include <iostream>
using namespace std;
struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; };
};
int Armstrong(int x)
{ int t=x,r,sum=0;
while(x)
{
r=x%10;
sum=sum+(r*r*r);
x=x/10;
}
if(t==sum)
return 1;
else
return 0;
}
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { cout << num_+i << '\n'; }
int num_;
};
void print_num(int i)
{
cout << i << '\n';
}
struct PrintNum {
void operator()(int i) const
{
cout << i << '\n';
}
};
int main()
{
boost::function<int (int x)> f;
boost::function<float ,int , int> s;
s=int_div();
f=&Armstrong;
cout<<"division result:";
cout<<s(5,2)<<endl;
cout<<"sum of results of two Armstrong numbers:";
cout<<f(153)+f(371)<<endl;
boost::function<void(int)> f_display = print_num;
cout<<"for free function:"<<endl;
f_display(-9);
boost::function<void()> f_display_42 = []() { print_num(42); };
cout<<"for boost::lambda:"<<endl;
f_display_42();
boost::function<void()> f_display_31337 = std::bind(print_num, 31337);
cout<<"for boost::bind: "<<endl;
f_display_31337();
cout<<"for member function:"<<endl;
boost::function<void(const Foo&, int)> f_add_display = &Foo::print_add;
const Foo foo(314159);
f_add_display(foo, 1);
f_add_display(314159, 1);
cout<<"for data member accessor:"<<endl;
boost::function<int(Foo const&)> f_num = &Foo::num_;
cout << "num_: " << f_num(foo) << '\n';
using std::placeholders::_1;
boost::function<void(int)> f_add_display2 = std::bind( &Foo::print_add, foo, _1 );
cout<<"for member function and object:"<<endl;
f_add_display2(2);
boost::function<void(int)> f_add_display3 = std::bind( &Foo::print_add, &foo, _1 );
cout<<"for member function and object ptr:"<<endl;
f_add_display3(3);
boost::function<void(int)> f_display_obj = PrintNum();
cout<<"for function object:"<<endl;
f_display_obj(18);
}
output:
division result:2.5
sum of results of two Armstrong numbers:2
for free function:
-9
for boost::lambda:
42
for boost::bind:
31337
for member function:
314160
314160
for data member accessor:
num_: 314159
for member function and object:
314161
for member function and object ptr:
314162
for function object:
18
Comments