C++ Boost Function














































C++ Boost Function



Executable in C++11/14/17
Description:

Boost.Function provides a class called boost::function which encapsulates function pointers. It is defined in boost/function.hpp.

It will take any object and any signature you provide in its template parameter, and create an object that's callable according to your signature and calls the object. If that's impossible, it's a compile error.

Header:
#include<boost/function.hpp>

Using boost::function in Code:

#include <boost/function.hpp>//for using boost function
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
boost::function<
int(const char*)> f = std::atoi;
//defines a pointer f that can point to functions
//that expect a parameter of type const char* and returns integer type
//std::atoi is used to accept integers from string
cout << f("120") << '\n';
f =
std::strlen;//Returns the length of the given byte string
cout << f("120") << '\n';
}

Output:

120
3

Comments