是std :: ofstream吗? [英] Is std::ofstream movable?

查看:245
本文介绍了是std :: ofstream吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个地图在MSVC10编译良好:

  std :: map< std :: string,std :: ofstream> m_logFiles; 

但是在ubuntu上使用g ++ 4.5启用C ++ 0x,我得到以下错误信息: / p>

/ usr / include / c ++ / 4.5 / bits / ios_base.h | 785 | error:'std :: ios_base :: ios_base(const std :: ios_base&)'是私人的



通过使用指针代替对象,我解决了这个问题。

在网上搜索,我了解到流不是要复制(为什么很好解释)。但我的问题是,std :: ofstream一个可移动类型?如果是,不应该允许它在标准容器中用作模板参数吗?

如果是,那么在这个点上是g ++在MSVC10之后吗? (这将解释为什么它在MSVC上工作)。我知道要求编译器作者完全实现甚至不是最终的东西是很愚蠢的,但我对未来充满好奇。



使用g ++ 4.6.1没有帮助。



编辑:阅读我进一步挖掘的评论,发现插入是导致问题,而不是地图的声明。 p>

阅读Cubbi的链接我试过以下:

  #include< string> ; 
#include< fstream>
#include< map>

using namespace std;

int main()
{
map< string,ofstream> m_logFiles;
ofstream st;
m_logFiles.insert(make_pair< string,ofstream>(string(a),move(st)));
return 0;
}

但还是没有运气。 g ++引用了b删除的拷贝构造函数的使用。

解决方案

std :: ofstream 是可移动的。这个程序编译为我使用clang / libc ++:

  #include< string> 
#include< fstream>
#include< map>

int main()
{
std :: map< std :: string,std :: ofstream> m_logFiles;
}

参考27.9.1.11 [ofstream.cons]。


I have this map which compiles fine in MSVC10 :

std::map<std::string, std::ofstream> m_logFiles;

But on ubuntu using g++ 4.5 with C++0x enabled, I get the following error message :

/usr/include/c++/4.5/bits/ios_base.h|785|error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private

By using pointers instead of objects, I resolved the problem.
Searching on the web, I learned that streams are not meant to be copied (the why was well explained). But my question is, is std::ofstream a movable type ? If it is, shouldn't it allow its use as a template parameter in the standard containers ?
If yes, then is g++ behind MSVC10 on this point ? (which would explain why it works on MSVC). I know it would be silly to ask compiler writers to fully implement something that isn't even final, but I'm curious regarding the future.

Using g++ 4.6.1 didn't help.

Edit : reading the comments I dug a little bit further and found that the insert is causing the problem, not the declaration of the map.

Reading Cubbi's link I tried the following :

#include <string>
#include <fstream>
#include <map>

using namespace std;

int main()
{
    map<string, ofstream> m_logFiles;
    ofstream st;
    m_logFiles.insert(make_pair<string, ofstream>(string("a"), move(st)));
    return 0;
}

But still no luck. g++ complains about the use of b deleted copy constructor.

解决方案

std::ofstream is movable. This program compiles for me using clang/libc++:

#include <string>
#include <fstream>
#include <map>

int main()
{
    std::map<std::string, std::ofstream> m_logFiles;
}

Reference 27.9.1.11 [ofstream.cons].

这篇关于是std :: ofstream吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆