gpt4 book ai didi

c++ - move 从 istream 派生的类

转载 作者:行者123 更新时间:2023-11-30 01:42:37 24 4
gpt4 key购买 nike

我正在创建一个带有自定义 streambuf 的 C++ istream。尝试 move 它失败,因为 istream move 构造函数受到保护。为了解决这个问题,我从 istream 派生了一个类:

struct VectorCharBuf : public streambuf {
VectorCharBuf(vector<char>& v) {
setg(v.data(), v.data(), v.data() + v.size());
}
};

struct IVectorCharStream : public istream {
IVectorCharStream(VectorCharBuf* contents_buf) : istream(contents_buf) {}
};

不生成此类的默认 move 构造函数,因为它涉及

use of deleted function 'std::basic_ios<_CharT, _Traits>::basic_ios(const std::basic_ios<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]'

此外,如果我尝试显式声明 move 构造函数,如

struct MyIStream : public istream {
MyIStream(MyIStream&& str) : istream(move(str)) {}
};

我收到错误消息“无效使用 void 表达式”。 (在最后一个案例中我可能做了一些愚蠢的事情,但我就是无法发现它......)

如何创建可 move 的 istream

最佳答案

struct MyIStream : public istream {
MyIStream(MyIStream&& str) : istream(move(str)) {}
};

I get an error "invalid use of void expression".

这不起作用,因为 basic_iosistream 的基类包含一个成员函数 void move(basic_ios& _Other)(用于 move 基类)。

如果您在构造函数中使用 std::move(str),它会编译!

关于c++ - move 从 istream 派生的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39394523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com