如何返回 fstream (C++0x)

发布于 2024-10-14 20:56:24 字数 1166 浏览 3 评论 0原文

我想我会直接进入它并从代码开始:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2024-10-21 20:56:24

我将在这里回答我自己的问题:

GCC C++0x 库中Features 页面,请查看第 27.9 项,其中内容如下:

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:

27.9 - File-based streams - Partial - Missing move and swap operations

I guess that's probably the issue I'm having with gcc.

挽梦忆笙歌 2024-10-21 20:56:24

问题在于:

test(test&& old) {};

这可以让你从右值 test 构造一个新的 test ,是的,但是它没有说明你的基础,它只是默认构造的(没有打开文件)。您想要的是这样的:

test(test&& old) : std::ofstream(std::move(old)) {};

这会将流从 old 移动到基础中。

The problem is with this:

test(test&& old) {};

This lets you construct a new test from an rvalue test, yes, but it says nothing about your base, which is simply being default constructed (no open file). What you want is this:

test(test&& old) : std::ofstream(std::move(old)) {};

Which will move the stream from old into the base.

美羊羊 2024-10-21 20:56:24

调用者是否需要知道您正在返回 ofstream,或者返回 streambuf 并让调用者将其包装在流中更有意义?

Does the caller need to know that you are returning an ofstream, or would it make more sense to return a streambuf, and let the caller wrap it inside a stream?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文