引入模版的定义关键字template,引入类型参数的关键字typename/class尽量用typename,类型参数一般用U,T,V等。
三.在编译过程中并不是把函数模版编译成能处理任何类型的实例,而是实例化模版参数的每种类型。
四.在实例化之前检查模版是否有语法错误,在实例化期间检查是否所有调用都有效。
bind1st bind2nd在stl里面有具体的实现,只是只能绑定两个参数。
boost里面的bind使用bind(Fun f,A1 a1,A2,a2...)产生一个对象,这个对象可以有占位符,可以等到调用函数对象的时候传递参数进去即通过bind b(..);b(args);
bind绑定参数是通过存储值的形式,如果对象很大,需要耗费很大的代价,可以用ref,但是bind是延后处理的,需要保证所引用的对象在调用之前不被释放。
一.绑定普通函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <string> #include <boost/algorithm/string.hpp> using namespace std; using namespace boost; int f( int a, int b) { return a+ b; } int g( int a, int b, int c) { return a + b *c; } int main() { cout << bind(f,1,2)()<<endl; cout << bind(g,1,2,3)() <<endl; return 0; } |
二.绑定成员函数
由于成员函数需要对象才能调用,所以需要占用bind的一个参数位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> #include <string> #include <boost/algorithm/string.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; struct demo { int f( int a, int b) { return a+ b; } }; int main() { demo a,&ra =a; demo *p = &a; cout << bind(&demo::f,a,_1,20)(10)<<endl; return 0; } |
三.通过for_each迭代传递对象给bind,循环调用被适配的成员函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <string> #include <vector> #include <boost/algorithm/string.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; struct demo { void print() { cout << "Hello" <<endl; } }; int main() { vector<demo> vec(10); for_each(vec.begin(),vec.end(),boost::BOOST_BIND(&demo::print,_1)); return 0; } |
四.绑定成员变量,通过迭代向函数传递参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #include <iostream> #include <string> #include <vector> #include <boost/algorithm/string.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost; struct demo { void print() { cout << "Hello" <<endl; } int x; }; void print( int x) { cout << x <<endl; } int main() { demo de; de.x=357; vector<demo> vec; for ( int i = 0;i < 10;++i) { vec.push_back(de); } vector< int > vec2(10); for_each(vec2.begin(),vec2.end(),bind(print,_1)); transform(vec.begin(),vec.end(),vec2.begin(),bind(&demo::x,_1)); for_each(vec2.begin(),vec2.end(),bind(print,_1)); return 0; } boost里的互斥量类型由mutex表示。 代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <iostream> #include <string> #include <vector> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; using namespace boost; int main() { mutex mu; try { this_thread::sleep(posix_time::seconds(2)); mu.lock(); //锁定cout对象 cout << "Some operations" <<endl; mu.unlock(); } catch ( int ) { mu.unlock(); return 0; } } |
二.上面的代码好像似曾相识,是的,在防止内存泄露的时候采用的和上面类似的处理方式,更加简洁的方式是智能指针,类似的我们需要用智能锁改写上面的代码scoped_lock智能锁。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <iostream> #include <string> #include <vector> #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; using namespace boost; template < typename T> class basic_atom:noncopyable { private : T n; typedef mutex mutex_t; mutex_t mu; public : basic_atom(T x = T()):n(x){} T operator++() { mutex_t::scoped_lock lock(mu); return ++n; } operator T(){ return n;} }; int main() { return 0; |
|