C++ boost::function














































C++ boost::function



Description:
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:
/* the below program shows all the basic usage of the boost::function with free function ,
lamda,bind,member functions, data member accessor.objects,function objects*/


#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; };
// as this function is explained in the third syntax
};

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) {} //this is the first syntax of the function definition
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;
// this is preferred syntax of boost::function form
boost::function<
float ,int , int> s;
// this is portable syntax of boost::function form

s=int_div();
//By default, function object wrappers are empty, so we can create a function object to assign to f

f=&
Armstrong;
// as the function is been directly accessed the function it is using reference operator

cout<<"division result:";
cout<<s(5,2)<<endl;
cout<<"sum of results of two Armstrong numbers:";
cout<<f(153)+f(371)<<endl;


// store a free function
boost::function<
void(int)> f_display = print_num;
cout<<"for free function:"<<endl;
f_display(
-9);

// store a lambda
boost::function<
void()> f_display_42 = []() { print_num(42); };
cout<<"for boost::lambda:"<<endl;
f_display_42();

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

// store a call to a member function
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);

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

// store a call to a member function and object
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);

// store a call to a member function and object ptr
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);

// store a call to a function object
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




More Articles of Konada Sunita:

Name Views Likes
C++ boost::accumulator::p_square_cumulative_distribution 672 1
C++ boost::Numeric Conversion::converter<> 864 10
C++ boost::ref 1227 9
C++ boost::accumulator::count 930 2
C++ boost::function::function_base 557 10
C++ boost::accumulator::weighted_mean 889 1
C++ boost::accumulator::non_coherent_weighted_tail_mean 563 1
C++ boost::accumulator::rolling_count rolling_sum rolling_mean rolling_moment 727 2
C++ boost::lockfree::spsc_queue 2202 10
C++ boost::coroutine:: passing data 640 1
C++ boost::accumulator::weighted_tail_quantile 619 1
C++ boost::TokenizerClass 83 2
C++ boost::accumulator::weighted_variance 559 1
C++ boost::accumulator::rolling mean 2595 2
C++ boost::accumulator::median 1417 1
C++ boost::accumulator::extended_p_square_quantile 837 1
C++ boost::accumulator::extended_p_square 648 1
C++ boost::function::functionN 829 10
C++ boost::accumulator::weighted_density 505 1
C++ boost::accumulator::covariance density 780 3
C++ boost::accumulator::weighted_skewness 503 1
C++ boost::format::formatter 654 10
C++ boost::accumulator::skewness 911 1
C++ boost::accumulator::max 700 3
C++ boost::accumulator::variance 992 1
C++ boost::accumulator::min 592 2
C++ boost::accumulator::density 951 1
C++ boost::token_iterator 885 10
c++ program to print root to leaf paths without using recursion 704 10
C++ boost::accumulator::rolling variance 1476 4
C++ boost::accumulator::weighted_sum 544 1
C++ boost::accumulator::weighted_extended_p_square_quantile 605 2
C++ boost::accumulator::sum_kahan 1117 2
C++ boost::accumulator::weighted_moment 469 1
C++ boost::accumulator::error_of 658 2
C++ boost::accumulator::tail 845 2
C++ boost::format 4138 10
C++ boost::accumulator::weighted_p_square_cumulative_distribution 522 1
C++ boost::accumulator::coherent_tail_mean 471 1
C++ boost::accumulator::rolling count 1153 2
C++ boost::accumulator::moment 703 2
C++ boost::lockfree::stack 1207 8
C++ boost::accumulator::sum 821 2
C++ boost::function::bad_function_call() 1096 10
C++ boost::accumulators::Mean Moment Count 1370 2
C++ boost::accumulator::mean 1617 2
C++ boost::accumulator::non_coherent_tail_mean 501 1
C++ boost::accumulator::weighted_extended_p_square 521 1
C++ boost::accumulator::weighted_kurtosis 522 1
C++ boost::accumulator::covariance density error_of_mean 587 9
C++ boost::accumulator::rolling sum 834 2
C++ boost::lockfree::queue 3084 9
C++ boost::accumulator::weighted_p_square_quantile 852 1
C++ boost::accumulator::p_square_quantile 840 1
C++ boost::accumulator::tail_quantile 1201 1
C++ boost::accumulator::rolling moment 598 2
C++ boost::accumulator::min max 1230 3
C++ boost::accumulator::kurtosis 1209 1
C++ boost::coroutine::Pushtype and pulltype 1564 1
C++ boost::accumulator::tail_variate 491 2
C++ boost::accumulator::weighted_covariance 519 2
C++ boost::format::Exception 2202 10
C++ boost::function 3035 10
C++ boost::NumericConversion::bounds<> 677 10
C++ boost::NumericConversion::UDT 853 10

Comments