显然,无法复制流.应该可以移动流.根据27.9.1.11 [ofstream.cons]段4有可能移动的构造std::ofstream(同为真std::ifstream,std::fstream和std::*stringstream变体).例如:
#include <iostream>
#include <fstream>
#include <string>
std::ofstream makeStream(std::string const& name) {
return std::ofstream(name);
}
int main()
{
std::ofstream out{ makeStream("example.log") };
}
Run Code Online (Sandbox Code Playgroud)
试图运动std::ostream,例如,有一个工厂函数创建一个std::ofstream,一个std::ostringstream,或者根据参数不起作用通过了URN一些其他的流.std::ostream(std::basic_ostream实际上,类模板)protected根据27.7.3.1 [ostream] 有一个移动构造函数.
为什么不能自己std::ostream移动?
我有这个地图,在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删除的拷贝构造函数.
我正在尝试导入我之前在Windows下使用C++ 11和OpenCV编写的项目,但它给了我麻烦,我无法弄清楚为什么.这是一个MakeFile项目,我添加了一行来启用C++ 11支持.但是,当我试图在eclipse中运行"make"或运行项目时,我收到以下错误(以及其他一些错误)
use of deleted function ‘std::basic_filebuf<char>& std::basic_filebuf<char>::operator=(const std::basic_filebuf<char>&)’ FacadeLabelingV2 line 599, external location: /usr/include/c++/4.8/fstream
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
#ifndef _FILEUTIL_CPP_
#define _FILEUTIL_CPP_
#include "Configuration.h"
#include "Utilities.cpp"
#include <iostream>
#include <fstream>
static void saveFeatures(const std::pair<cv::Mat, cv::Mat>& features, const Configuration& config, bool training, bool append, int counter = 0){
string prefix;
if (training) {
prefix = "train";
} else {
prefix = "test";
}
std::string directory = config.dir_classifiers + config.name_of_run;
std::ofstream save_file;
std::string counter_appendix = std::to_string(counter / 50);
std::string path_temp = directory + …Run Code Online (Sandbox Code Playgroud) 在 现代 C++ 风格的要点(视频)中,Herb Sutter 提倡:
auto varname = Constructor{ ... };
Run Code Online (Sandbox Code Playgroud)
代替
Constructor varname( ... );
Run Code Online (Sandbox Code Playgroud)
我试着做:
auto log = fstream{"log.txt", fstream::out};
Run Code Online (Sandbox Code Playgroud)
但收到一条g++4.8 -std=c++11关于已删除函数的错误消息(无论我使用大括号还是圆括号)。
是我的错还是g++的错?
fstream log{"log.txt", fstream::out};
Run Code Online (Sandbox Code Playgroud)
工作正常。
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
auto log = fstream{"log.txt", fstream::out};
//fstream log{"log.txt", fstream::out};
log << "hello world" << endl;
}
Run Code Online (Sandbox Code Playgroud)
g++ -std=c++11 1.cc -o 1
1.cc: In function ‘int main(int, char**)’:
1.cc:6:47: error: use of deleted function ‘std::basic_fstream<char>::basic_fstream(const std::basic_fstream<char>&)’ …Run Code Online (Sandbox Code Playgroud)