代码段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, in…
直接代码: 代码段1: #include <iostream> #include <string> #include <boost/bind/bind.hpp> class some_class { public: typedef void result_type; void print_string(const std::string& s) const { std::cout << s << '\n'; } }; void print…
第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #include <iostream> #include <algorithm> #include <functional> #include <vector> #include <boost/bind/bind.hpp> #include <bo…
转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不来了”,而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boost::function和boost::bind已经纳入了std::tr1,这或许是C++0x最值得期待的功能,它将彻底改变C++库的设计方式,以及应用程序的编写方式. Scott Meyer…
以boost::function和boost:bind取代虚函数 这是一篇比较情绪化的blog,中心思想是"继承就像一条贼船,上去就下不来了",而借助boost::function和boost::bind,大多数情况下,你都不用上贼船. boost::function和boost::bind已经纳入了std::tr1,这或许是C++0x最值得期待的功能,它将彻底改变C++库的设计方式,以及应用程序的编写方式. Scott Meyers的Effective C++ 3rd ed.第35条…
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Function 库包含了一个类族的函数对象的包装.它的概念很像广义上的回调函数.其有着和函数指针相同的特性但是又包含了一个调用的接口.一个函数指针能够在能以地方被调用或者作为一个回调函数.boost.function能够代替函数指针并提供更大的灵活性. 2. 使用 Boost.Function 有两种形式…
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期冀对他人也有用处. 注:本文暂时不探索bind和function的实现和开销. 1. bind 是什么 boost::bind 是std::bindlist 和 std::bind2nd的结合体.它提供一个任意的函数对象(仿函数).函数.函数指针.成员函数指针. 它可以绑定任意的参数.bind 没有…
最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代替拥有相同返回类型,相同参数类型,以及相同参数个数的各个不同的函数. #include<boost/function.hpp> #include<iostream> typedef boost::function<int(int ,char)> Func; int test…
http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从 "boost/function/function0.hpp" 到 "boost/function/function10.hpp". 如果你知道你想保存在 function 中的函数的参数数量,这样做可以让编译器…
为atoi取别名fun,fun实质上是函数指针 #include <iostream> #include <boost/function.hpp> void main() { boost::function<int(char *)>fun = atoi;//为atoi取别名fun,fun实质上是函数指针 std::cout << fun(") << std::endl; fun = strlen; std::cout <<…