Listing 6
#include <functional>
#include <iostream>
#include <math.h>
using std::cout;
using std::tr1::bind;
using namespace std::tr1::placeholders;
template <class Fun>
void call_zero(Fun fun)
{ // call function object with no arguments
cout << ' ' << fun() << '\n';
}
template <class Fun>
void call_one(Fun fun)
{ // call function argument with one argument
double arg = 1.0;
cout << ' ' << fun(arg) << '\n';
}
template <class Fun>
void call_two(Fun fun)
{ // call function argument with two arguments
double arg1 = 1.0;
double arg2 = 2.0;
cout << ' ' << fun(arg1, arg2) << '\n';
}
int main()
{ // demonstrate use of bind
cout << "calling cosf:\n";
call_zero(bind(cosf, 1.0)); // object calls cosf(1.0)
call_one(bind(cosf, _1)); // object calls cosf with first argument
call_two(bind(cosf, _2)); // object calls cosf with second argument
return 0;
}