您的位置:首页 > 编程语言 > C语言/C++

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

2014-05-09 13:25 459 查看
代码段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(5, 3) << std::endl;
if (f)
std::cout << f(5, 3) << std::endl;
else
std::cout << "f has no target, so it is unsafe to call" << std::endl;
f = 0;
f = &mul_ints;
if (!f.empty())
{
std::cout << f(6, 4) << 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(5, 3) << std::endl;

//error
//f = &int_div();
//std::cout << f(5, 3) << std::endl;

return 0;
}


代码段2:

#include <boost/function.hpp>
#include <iostream>

void do_sum_avg(int values[], int n, int& sum, float& avg)
{
sum = 0;
for (int i = 0; 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[5] = {4, 5, 6, 9, 3};
int cnArr = sizeof(arr)/sizeof(int);
int sum = 0;
float avg = 0.0;
sum_avg(arr, cnArr, sum, avg);
std::cout << "arr, " << sum << ", " << avg << std::endl;

return 0;
}


代码段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, 5) == -5);
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(5); // Call x.foo(5)

return 0;
}


代码段5:

#include <boost/function.hpp>
#include <iostream>

struct stateful_type { int operator()(int) const { return 0; } };

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();                                    //1
f2 = 0;                                        //2

return 0;
}


代码段6:

#include <boost/test/minimal.hpp>
#include <boost/function.hpp>
#include <iostream>

struct stateful_type { int operator()(int) const { return 0; } };

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(5) << std::endl;
f.clear();

f = boost::ref(stateful_type());//ok
BOOST_CHECK(!f.empty());
std::cout << f(5) << std::endl;
f.clear();

//f = boost::ref(stateful_type);//error

f = stateful_type();//ok
BOOST_CHECK(!f.empty());
std::cout << f(5) << std::endl;
f.clear();

boost::function<int (int)> f2(f);

return 0;
}


代码段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 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: