如何返回 fstream (C++0x)
我想我会直接进入它并从代码开始:
#include <iostream>
#include <fstream>
#include <string>
class test : public std::ofstream
{
public:
test(const std::string& filename) { this->open(gen_filename(filename)); };
test(const test&) = delete;
//test(test&& old) = default; // Didn't compile
test(test&& old) {};
private:
std::string gen_filename(const std::string& filename)
{ return filename + ".tmp"; }
};
int main()
{
auto os = test("testfile");
os << "Test1\n";
os << "Test2\n";
}
基本上,我需要返回一个 ofstream。当然,你不能复制 ofstream,所以我摆弄了类测试中的代码,并且我得到了上面的编译和工作,如你所期望的(在 gcc 4.5 上)。
但我有一种不好的感觉,这只是因为我的编译器在“auto os = test()”上执行了“返回值优化”(RTO)。事实上,如果修改为以下内容:
int main()
{
auto os = test("testfile");
os << "Test1\n";
auto os2 = std::move(os);
os2 << "Test2\n";
}
我不再在输出中同时得到 Test1 和 Test2。
问题是,“test”类不可复制,因此 ofstream 不可能被复制。我只是希望能够从函数返回它。我似乎能够通过 GCC 做到这一点。
我不想取消引用指向分配的流堆的智能指针,或者重新打开文件,因为它当前无需执行这些操作即可工作。我只是觉得我的方法有点“不标准”,所以用标准方法来做我所描述的事情会很棒。
I think I'll get right into it and start with the code:
#include <iostream>
#include <fstream>
#include <string>
class test : public std::ofstream
{
public:
test(const std::string& filename) { this->open(gen_filename(filename)); };
test(const test&) = delete;
//test(test&& old) = default; // Didn't compile
test(test&& old) {};
private:
std::string gen_filename(const std::string& filename)
{ return filename + ".tmp"; }
};
int main()
{
auto os = test("testfile");
os << "Test1\n";
os << "Test2\n";
}
Basically, I need to return an ofstream. Of course you can't copy an ofstream, so I fiddled around with the code in the class test, and I got the above to compile and work as you would expect (on gcc 4.5).
But I have a bad feeling this is just due to my compiler doing "Return Value Optimization" (RTO) on "auto os = test()". Indeed, if modify to the following:
int main()
{
auto os = test("testfile");
os << "Test1\n";
auto os2 = std::move(os);
os2 << "Test2\n";
}
I no longer get both Test1 and Test2 in the output.
The thing is, the class "test" isn't copyable, so there's no chance of the ofstream being duplicated. I just want to be able to return it from a function. And I seem to be able to do that with GCC.
I'd rather not have dereference smart pointers to a heap allocated ofstream, or reopen the file, as it currently works without doing those things. I just have a feeling I'm being a little "non-standard" in my approach, so a standard way of doing what I've described would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将在这里回答我自己的问题:
在 GCC C++0x 库中Features 页面,请查看第 27.9 项,其中内容如下:
我想这可能是我在使用 gcc 时遇到的问题。
I'm going to answer my own question here:
In the GCC C++0x Library Features page, have a look at item 27.9, which reads:
I guess that's probably the issue I'm having with gcc.
问题在于:
这可以让你从右值
test
构造一个新的test
,是的,但是它没有说明你的基础,它只是默认构造的(没有打开文件)。您想要的是这样的:这会将流从
old
移动到基础中。The problem is with this:
This lets you construct a new
test
from an rvaluetest
, yes, but it says nothing about your base, which is simply being default constructed (no open file). What you want is this:Which will move the stream from
old
into the base.调用者是否需要知道您正在返回
ofstream
,或者返回streambuf
并让调用者将其包装在流中更有意义?Does the caller need to know that you are returning an
ofstream
, or would it make more sense to return astreambuf
, and let the caller wrap it inside a stream?