boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

时间:2022-01-28 09:03:52

代码段1:

 #include <boost/function.hpp>
#include <iostream> float mul_ints(int x, int y) { return ((float)x) * y; }
struct int_div {
float operator()(int x, int y) const { return ((float)x)/y; };
}; int main()
{
boost::function<float (int x, int y)> f;
f = int_div();
std::cout << f(, ) << std::endl;
if (f)
std::cout << f(, ) << std::endl;
else
std::cout << "f has no target, so it is unsafe to call" << std::endl;
f = ;
f = &mul_ints;
if (!f.empty())
{
std::cout << f(, ) << std::endl;
}
else
{
std::cout << "f has no target, so it is unsafe to call" << std::endl;
} f = boost::ref(int_div());
std::cout << f(, ) << std::endl; //error
//f = &int_div();
//std::cout << f(5, 3) << std::endl; return ;
}

代码段2:

 #include <boost/function.hpp>
#include <iostream> void do_sum_avg(int values[], int n, int& sum, float& avg)
{
sum = ;
for (int i = ; i < n; i++)
sum += values[i];
avg = (float)sum / n;
}
int main()
{
//boost::function<void (int values[], int n, int& sum, float& avg)> sum_avg; //1,表意清晰
//boost::function<void (int *values, int n, int& sum, float& avg)> sum_avg; //2,同义
boost::function<void (int *, int , int& , float& )> sum_avg; //3,无参数,表意不清晰 //sum_avg = &do_sum_avg; //1,对它取指针
//sum_avg = boost::ref(do_sum_avg); //2,对它的引用
sum_avg = do_sum_avg; //3,这样写不严谨 int arr[] = {, , , , };
int cnArr = sizeof(arr)/sizeof(int);
int sum = ;
float avg = 0.0;
sum_avg(arr, cnArr, sum, avg);
std::cout << "arr, " << sum << ", " << avg << std::endl; return ;
}

代码段3:

 #include <boost/function.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <iostream>
#include <functional> struct X {
int foo(int);
std::ostream& foo2(std::ostream&) const;
};
int X::foo(int x) { return -x; }
std::ostream& X::foo2(std::ostream& x) const { return x; } int main()
{
boost::function<int (X*, int)> f;
boost::function<std::ostream& (X*, std::ostream&)> f2; f = &X::foo;
//f = &boost::ref(X::foo);//error
//f = boost::ref(&X::foo);//error
f2 = &X::foo2; X x;
BOOST_TEST(f(&x, ) == -);
BOOST_TEST(f2(&x, boost::ref(std::cout)) == std::cout); return ::boost::report_errors();
}

代码段4:

 #include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/mem_fn.hpp>
#include <iostream>
#include <functional> struct X {
int foo(int);
};
int X::foo(int x) { return -x; } int main()
{
boost::function<int (int)> f;
X x;
//f = std::bind1st(std::mem_fun(&X::foo), &x); //1 ok
//f = boost::mem_fn(boost::bind(&X::foo, &x)); //2 error
//f = std::bind1st(boost::mem_fn(&X::foo), &x); //3 ok
//f = std::bind(boost::mem_fn(&X::foo), &x); //4 error
//f = std::bind(&X::foo, &x, _1); //5 error f(); // Call x.foo(5) return ;
}

代码段5:

 #include <boost/function.hpp>
#include <iostream> struct stateful_type { int operator()(int) const { return ; } }; int main()
{
stateful_type a_function_object;
boost::function<int (int)> f;
f = boost::ref(a_function_object); boost::function<int (int)> f2(f); f2.clear(); //
f2 = ; // return ;
}

代码段6:

 #include <boost/test/minimal.hpp>
#include <boost/function.hpp>
#include <iostream> struct stateful_type { int operator()(int) const { return ; } }; int test_main(int, char*[])
//int main()
{
stateful_type a_function_object;
boost::function<int (int)> f;
f = boost::ref(a_function_object);//error?
BOOST_CHECK(!f.empty());
std::cout << f() << std::endl;
f.clear(); f = boost::ref(stateful_type());//ok
BOOST_CHECK(!f.empty());
std::cout << f() << std::endl;
f.clear(); //f = boost::ref(stateful_type);//error f = stateful_type();//ok
BOOST_CHECK(!f.empty());
std::cout << f() << std::endl;
f.clear(); boost::function<int (int)> f2(f); return ;
}

代码段7:

 #include <boost/test/minimal.hpp>
#include <boost/function.hpp> using namespace std;
using namespace boost; static int bad_fn(float f) { return static_cast<int>(f); } int
test_main(int, char*[])
{
function0<int> f1;
f1 = bad_fn; BOOST_ERROR("This should not have compiled."); return ;
}