std :: ofstream是否可移动?

Jon*_*let 10 c++ ofstream c++11

我有这个地图,在MSVC10中编译得很好:

std::map<std::string, std::ofstream> m_logFiles;
Run Code Online (Sandbox Code Playgroud)

但是在使用g ++ 4.5并启用了C++ 0x的ubuntu上,我收到以下错误消息:

/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没有帮助.

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

阅读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;
}
Run Code Online (Sandbox Code Playgroud)

但仍然没有运气.g ++抱怨使用b删除的拷贝构造函数.

How*_*ant 8

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

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

int main()
{
    std::map<std::string, std::ofstream> m_logFiles;
}
Run Code Online (Sandbox Code Playgroud)

参考27.9.1.11 [ofstream.cons].

  • 任何一个编译器(甚至许多编译器)的结果都不能真正回答这个问题.需要参考规范. (6认同)
  • @Nemo - Howard是为这个编译器实现标准库的人.我相信他. (5认同)
  • @Bo:谢谢Bo.Fwiw,我也是那个让'osftream'可以移动的人.如果不是,我的心情会非常糟糕.:-) (4认同)

Cli*_*ton 6

我之前问了一个类似的问题,后来发现GCC似乎还不支持可移动的fstream(我刚刚测试了GCC 4.6.1),详见答案.