[Boost基础]函数与回调――ref引用

2014-11-24 08:25:40 · 作者: · 浏览: 0
库ref在boost/ref.hpp中提供了模板工厂函数boost::ref,boost::cref分别对应包装引用和常引用。
当在某些情况下需要拷贝对象参数,如果该对象无法进行拷贝,或者拷贝代价过高,这时候就可以选择ref。
[cpp]
#pragma once
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace boost;
//boost.ref应用代理模式,引入对象引用的包装器(reference_wrapper)概念解决了这类问题(一般情况下传值语义都是可行的,但也有很多特殊情况,作为参数的函数对象拷贝代价过高,或者不希望拷贝对象,甚至拷贝是不可行的(单件))
void test1()
{
int x=10;
reference_wrapper rw(x);//包装int类型的引用
assert(x==rw);//隐身转换为int类型
(int&)rw=100;//显示转换为int&类型,用于左值
reference_wrapper rw2(rw);//拷贝构造函数
assert(rw2.get()==100);
string str;
reference_wrapper rws(str);//包装字符串的引用
*rws.get_pointer()="test reference_wrapper";//指针操作
cout<<"str: "<
//reference_wrapper的用法类似于C++在那个的引用类型(T&),就像是被包装对象的别名。为此,必须赋值初始化,就像是使用一个引用类型的变量。它提供了get()和get_pointer()分别返回存储的引用和指针。
}
//reference_wrapper的名字过长,声明引用包装对象很不方便,因而ref库提供了两个便捷的工厂函数ref()和cref()(const ref()),可以通过参数类型很容易推导出构造reference_wrapper对象。
void test2()
{
// vectorv(10,2);
// BOOST_AUTO(rw,cref(v));
// assert(is_reference_wrapper::value);
// assert(!is_reference_wrapper::value);
// string str;
// BOOST_AUTO(rws,ref(str));
// cout<::type).name()<
// cout<::type).name()<
}
#include
#include
#include
#include
#include
void print(std::ostream& os,int i)
{
os<
}
void test3()
{
//std::cout为标准输出对象,它是无法进行拷贝的,因此此处必须使用boost::ref来绑定它的引用,否则会提示拷贝构造失败,因为拷贝构造是私有的。
boost::functionpt=boost::bind(print,boost::ref(std::cout),_1);
vectorv(1,1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
std::for_each(v.begin(),v.end(),pt);//1 2 3 4
}
void test(char t)
{
cout<<"press key====="<
switch (t)
{
case '1':test1();break;
case '2':test2();break;
case '3':test3();break;
// case '4':test4();break;
case 27: www.2cto.com
case 'q':exit(0);break;
default: cout<<"default "<
}
}
int main()
{
while(1)
{
test(getch());
}
return 0;
}