关于boost::bind中fstream对象禁止拷贝的解决方法
来源:互联网 发布:风险管理矩阵 编辑:程序博客网 时间:2024/05/01 05:37
fstream的拷贝构造函数是私有的,禁止fstream对象的拷贝。
比如,下面的程序编译出错,提示拷贝构造函数私有:
#include<fstream>#include<iostream>#include<boost/thread/thread.hpp>using namespace std;void fun(ofstream &out){std::cout<<"succeed!"<<endl;}int main(){ofstream coutt("a.txt");fun(coutt);boost::bind(&fun,coutt);}
编译结果:
解决办法是用std::ref(或者boost::ref)将fstream对象包装。改过后的代码如下:
#include<fstream>#include<iostream>#include<boost/thread/thread.hpp>#include<boost/bind.hpp>using namespace std;void fun(ofstream &out){std::cout<<"succeed!"<<endl;}int main(){ofstream coutt("a.txt");fun(coutt);boost::bind(&fun,boost::ref(coutt));}
再次编译,可以通过。
注意:使用std::ref需要包含头文件#include<functional>
std::ref 用于包装按引用传递的值。
std::cref 用于包装按const 引用传递的值。
参考:http://www.cppblog.com/everett/archive/2012/12/03/195939.html 0 0
- 关于boost::bind中fstream对象禁止拷贝的解决方法
- boost::bind会返回一个函数对象,它内部保存了数据的拷贝
- boost::noncopyable 禁止拷贝
- boost中bind的使用
- boost中bind的使用
- 【Boost】boost库中bind的用法
- Boost关于bind的使用以及函数对象和传递参数的问题
- 关于boost::function与boost::bind函数的使用心得
- 关于boost::function与boost::bind函数的使用心得
- 详细解析boost中bind的实现
- 详细解析boost中bind的实现 .
- boost库中bind的用法
- 详细解析boost中bind的实现
- C++禁止对象拷贝
- fstream对象作为函数参数的问题汇总及解决方法
- 【boost】boost::bind的使用
- boost::bind的使用
- boost bind的使用
- HTML的相关路径与绝对路径的问题
- 002-创建的Eclipse项目中红叹号问题解决思路
- ubuntu下常用操作
- 怎样使一个Android应用不被杀死?
- 【数据结构】静态查找之分块查找
- 关于boost::bind中fstream对象禁止拷贝的解决方法
- jvm 监控工具
- CocoaPods管理第三方开源组件,入门介绍
- Android 动画总结
- linux下安装配置Memcache和PHP环境
- 约瑟夫环问题
- linux启动过程
- 基于ibatis插入Blob数据
- hdu 2546 饭卡