重点内容
#if 0
namespace 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;
};
// constructors
reference_wrapper<T> ref(T&);
reference_wrapper<T const> cref(T const&);
// access
unwrap_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);
}