boost—— 函数与回调ref小工具类
来源:互联网 发布:思科网络防火墙5506 编辑:程序博客网 时间:2024/06/08 13:44
重点内容
#if 0namespace boost { template<typename T> class reference_wrapper; reference_wrapper<T> ref(T&); reference_wrapper<T const> cref(T const&); unwrap_reference<T>::type& unwrap_ref(T&); template<typename T> class is_reference_wrapper; template<typename T> class unwrap_reference;}reference_wrapper摘要:template<typename T> class reference_wrapper {public: // types typedef T type; // construct/copy/destruct explicit reference_wrapper(T&); // access operator T&() const; T& get() const; T* get_pointer() const;private: T*t;};// constructorsreference_wrapper<T> ref(T&);reference_wrapper<T const> cref(T const&);// accessunwrap_reference<T>::type& unwrap_ref(T&);/*reference_wrapper支持拷贝构造和赋值,而引用类型是不可赋值的*/#endif
#include <iostream>#include <string>#include <vector>#include <set>#include <map>#include <algorithm>#include <boost/ref.hpp>#include <boost/assign.hpp>#include <math.h>#include <typeinfo>#include <boost/typeof/typeof.hpp>using namespace boost;using namespace std;int main( int argc,char **argv){ int x = 20; reference_wrapper<int> rw(x); //包装类型的引用 assert(x = rw); //隐式转换为int类型 (int &)rw = 1000; //显示转换为int&类型,用于左值 assert(1000 == x); reference_wrapper<int> rw2(rw); //拷贝构造 assert(1000 == rw2.get()); string str; reference_wrapper<string> rws(str); //包装字符串的引用 *rws.get_pointer() = "hello world"; //指针操作 cout << rws.get().size() << endl; /*这里的get函数先获得被包装的真正的对象,然后调用其方法。如果和下面一样,则会引发编译错误 cout << rws.size() << endl*/ /*reference_wrapper的两个工厂函数*/ double x1 = 2.4448; BOOST_AUTO(rw3,cref(x)); //包装double const cout << typeid(rw3).name() << endl; string str1; BOOST_AUTO(rws2,ref(str1)); //包装字符串 cout << typeid(rws2).name() << endl; cout << sqrt(cref(x1)) << endl; //直接利用其的拷贝,然后直接计算 /*操作包装的两个类*/ vector<int> iv(10,3); assert(is_reference_wrapper<BOOST_TYPEOF(cref(iv))>::value); //经过包装 assert(!is_reference_wrapper<BOOST_TYPEOF(iv)>::value); //未经包装 string str2; /*is_reference_wrapper的bool成员变量value可以判断其是否经过包装 * unwrap_reference无论是否经过包装,都可以通过内部类型确定T的真实类型*/ cout << typeid(unwrap_reference<BOOST_TYPEOF(ref(str))>::type).name() << endl; cout << typeid(unwrap_reference<BOOST_TYPEOF(str2)>::type).name() << endl; /*unwrap_ref解开包装,返回被包装对象的引用*/ set<double> sd; BOOST_AUTO(rw5,ref(sd)); //获得一个包装 unwrap_ref(rw5).insert(13.98); //解包并插入对象 string str12("hello world"); BOOST_AUTO(rws12,cref(str12)); cout << unwrap_ref(rws) << endl; //解包 cout << unwrap_ref(str12) << endl; //对未包装对象解包 return (0);}
ref的使用实例,包装引用
#include <iostream>#include <string>#include <vector>#include <set>#include <map>#include <algorithm>#include <boost/ref.hpp>#include <boost/assign.hpp>#include <math.h>#include <typeinfo>#include <boost/typeof/typeof.hpp>using namespace boost;using namespace std;class big_class{private: int x;public: big_class():x(0){ } void print() { cout << "big_class" << ++x << endl; } };template<typename T>void print(T a){ for(int i = 0;i < 2;++i){ unwrap_ref(a).print(); }}int main(int argc,char **argv){ big_class c; BOOST_AUTO(rw,ref(c)); c.print(); print(c); print(rw); print(c); c.print(); return (0);}
0 0
- boost—— 函数与回调ref小工具类
- boost—— 函数与回调result_of小工具类
- [Boost基础]函数与回调——ref引用
- [Boost基础]函数与回调——ref引用
- 【Boost】boost库中的小工具enable_shared_from_this
- 【备忘录】环境配置与小工具类
- [Boost基础]函数与回调——function函数
- 小工具推介——完美卸载
- nodejs 小工具——supervisor
- 小工具类
- Java 小工具类
- java小工具类
- 一个小工具类
- [Boost基础]函数与回调——bind绑定
- boost—— 函数与回调bind
- boost—— 函数与回调function
- 3.2 让机器动起来——框架启动与小工具初始化
- 杂七杂八 android小工具类
- 开发商问题的领域术语表建立
- strncpy()函数的介绍
- 在Windows下访问Ubuntu共享文件夹
- 第十五周项目G按要求插入字符串中
- 编译MeshLab(4)—VS2010编译MeshLab源码之编译meshlab_full.pro
- boost—— 函数与回调ref小工具类
- 如何动态设置单元格的左主格
- 移动端是指手机浏览器,hybrid native
- 深入JVM-java虚拟机的基本结构
- Linux C工程管理 之autoconf与automake
- pcre-8.36/missing:行81: aclocal-1.14: 未找到命令
- Method Reference in Java 8
- caffe安装吐血总结 opencv的问题,ubuntu版本问题
- 知识库--The Loader Interface(47)