代码段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 ;
}

boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》的更多相关文章

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

    直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> c ...

  2. boost::bind实践

    第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...

  3. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  4. boost::function和boost:bind取代虚函数

    以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function ...

  5. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  6. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  7. 关于boost::function与boost::bind函数的使用心得

    最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...

  8. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

  9. #include <boost/function.hpp>

    为atoi取别名fun,fun实质上是函数指针 #include <iostream> #include <boost/function.hpp> void main() { ...

随机推荐

  1. 关于ArrayList线程安全解决方案

    一:使用synchronized关键字 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List<Map<String,Obje ...

  2. [Unix.C]Files and Directories

    stat, fstat, and lstat Functions  本部分讨论的内容主要围绕3个stat函数及其返回值. #include <sys/stat.h> int stat(co ...

  3. 使用alloctor模板来实现string类

    虽然以前做过更复杂的各种数据结构,不过那只是在看完c++prime7章后做的,没有考虑到类的拷贝体现出来是类值还是类指针,于是写了一些半成品类,不过那些主要是练数据结构,不想再改,于是就想办法模仿了下 ...

  4. 理解runtime system

    最近需要编译不同平台的库,因此比以前只开发C++程序关注底层更多点.先来看看术语runtime system的解释. 主要参考资料: http://en.wikipedia.org/wiki/Runt ...

  5. 微信小程序资料集合

    一:官方地址集合: 1:官方工具:https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=1476434678461 2: ...

  6. Android开发艺术探索(一)——Activity的生命周期和启动模式

    Activity的生命周期和启动模式 生命周期有? 1.典型情况下的生命周期—>指有用户参与的情况下,Activity所经过的生命周期改变 2.异常情况下的生命周期—>指Activity被 ...

  7. Jsp学习(1)

    Servlet的用作:用java语言开发动态资源的技术: Jsp的作用:用java语言(+html)开发动态的资源,其实jsp就是servlet演化而来的. Jsp的执行过程: 我们先来做一个实验,首 ...

  8. ASP.NET多线程下使用HttpContext.Current为null解决方案

    多线程或者异步调用中如何访问HttpContext? 前面我还提到在APM模式下的异步完成回调时,访问HttpContext.Current也会返回null,那么此时该怎么办呢? 答案有二种:1. 在 ...

  9. 学习PHP时的一些总结(二)

    类中的构造方法和析构方法: 构造方法是对象创建完成后第一个被对象自动调用的方法.析构方法是对象在销毁之前最后一个被对象自动调用的方法. 如果没有显示的声明构造方法,类中都会默认存在一个没有参数列表并且 ...

  10. myeclipse 编码问题

    在使用eclipse+MyEclipse开发中,许多文件编码默认是ISO-8859-1,不支持中文(如常用的JSP),这样我们每次建文件都要手动改编码,其实我们可以在设置文件默认编码,今后再创建时就不 ...