小编Jon*_*let的帖子

std :: ofstream是否可移动?

我有这个地图,在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删除的拷贝构造函数.

c++ ofstream c++11

10
推荐指数
2
解决办法
2673
查看次数

g ++和Visual Studio中方法指针转换规则之间的差异

struct Test
{
    typedef unsigned (Test::*MethodPtr)();
    unsigned testMethod() {}
};
typedef void (*ThreadPtr)(void *);
ThreadPtr threadPtr = reinterpret_cast<ThreadPtr>(&Test::testMethod);
Run Code Online (Sandbox Code Playgroud)

我想将一个线程发送到特定对象的类方法中.我使用方法指针作为线程入口点,并将对象指针作为唯一参数传递.这可行,因为我的结构中没有任何虚拟声明.

我的问题与reinterpret_cast操作有关.g ++允许这样,Visual Studio 2008则不然.我通过将方法指针值直接memcp到threadPtr变量来解决VS2008的限制.生成的代码工作正常但是这应该是一个简单的操作是一个非常可怕的解决方法.谁能提供更优雅的替代品?

谢谢

-G

编辑:

为了澄清,gcc给出的警告如下:

methodPtrTest.cpp:14: warning: converting from ‘void (Test::*)()’ to ‘void (*)(void*)’
Run Code Online (Sandbox Code Playgroud)

c++ methods pointers g++ visual-studio-2008

0
推荐指数
1
解决办法
381
查看次数

标签 统计

c++ ×2

c++11 ×1

g++ ×1

methods ×1

ofstream ×1

pointers ×1

visual-studio-2008 ×1