[Boost基础]函数与回调——ref引述

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

库ref在boost/ref.hpp中提供了模板工厂函数boost::ref,boost::cref分别对应包装引用和常引用。

当在某些情况下需要拷贝对象参数,如果该对象无法进行拷贝,或者拷贝代价过高,这时候就可以选择ref。 

#pragma  once
#include <boost/ref.hpp>
#include <boost/assert.hpp>
#include <string>
#include <iostream>
#include <conio.h>
#include <vector>
using namespace std; 
using namespace boost;
//boost.ref应用代理模式,引入对象引用的包装器(reference_wrapper)概念解决了这类问题(一般情况下传值语义都是可行的,但也有很多特殊情况,作为参数的函数对象拷贝代价过高,或者不希望拷贝对象,甚至拷贝是不可行的(单件))
void test1()
{     
	int x=10;
	reference_wrapper<int> rw(x);//包装int类型的引用
	assert(x==rw);//隐身转换为int类型
	(int&)rw=100;//显示转换为int&类型,用于左值
	reference_wrapper<int> rw2(rw);//拷贝构造函数
	assert(rw2.get()==100);
	string str;
	reference_wrapper<string> rws(str);//包装字符串的引用
	*rws.get_pointer()="test reference_wrapper";//指针操作
	cout<<"str: "<<str<<"  size:"<<rws.get().size()<<endl;//str:test reference_wrapper size:22
	//reference_wrapper的用法类似于C++在那个的引用类型(T&),就像是被包装对象的别名。为此,必须赋值初始化,就像是使用一个引用类型的变量。它提供了get()和get_pointer()分别返回存储的引用和指针。
} 
//reference_wrapper的名字过长,声明引用包装对象很不方便,因而ref库提供了两个便捷的工厂函数ref()和cref()(const ref()),可以通过参数类型很容易推导出构造reference_wrapper对象。
void test2()
{  
//	vector<int>v(10,2);
// 	BOOST_AUTO(rw,cref(v));
// 	assert(is_reference_wrapper<BOOST_TYPEOF(rw)>::value);
// 	assert(!is_reference_wrapper<BOOST_TYPEOF(v)>::value);
// 	string str;
// 	BOOST_AUTO(rws,ref(str));
// 	cout<<typeid(unwrap_reference< BOOST_TYPEOF<rws>::type).name()<<endl;
// 	cout<<typeid(unwrap_reference< BOOST_TYPEOF<str>::type).name()<<endl;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
#include <boost/function.hpp>
void print(std::ostream& os,int i)
{
	os<<i<<std::endl;
}
void test3()
{ 
	//std::cout为标准输出对象,它是无法进行拷贝的,因此此处必须使用boost::ref来绑定它的引用,否则会提示拷贝构造失败,因为拷贝构造是私有的。
	boost::function<void (int)>pt=boost::bind(print,boost::ref(std::cout),_1);
	vector<int>v(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====="<<t<<endl;
	switch (t)
	{ 
	case '1':test1();break;
 	case '2':test2();break;
	case '3':test3();break;
		// 	case '4':test4();break;
	case 27:
	case 'q':exit(0);break;
	default: cout<<"default "<<t<<endl;break;
	}
}
int main()
{
	while(1)
	{
		test(getch());
	} 
	return 0;
}