问题描述
我使用自定义 streambuf
创建了一个C ++ istream
。尝试移动此失败,因为istream移动构造函数是受保护的。为了绕过这个我从 istream
得到一个类:
I'm creating a C++ istream
with a custom streambuf
. Trying to move this fails because the istream move constructor is protected. To get round this I derived a class from 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) {}
};
不会生成此类的默认移动构造函数,因为它涉及
The default move constructor for this class is not generated because it involves the
此外,如果我尝试明确声明一个move构造函数,如
Further, if I try to declare a move constructor explicitly, as in
struct MyIStream : public istream {
MyIStream(MyIStream&& str) : istream(move(str)) {}
};
我得到一个错误无效使用void expression。 (我可能在这后一种情况下做蠢事,但我只是不能发现它。)
I get an error "invalid use of void expression". (I'm probably doing something silly in this last case, but I just can't spot it... .)
如何创建一个可移动 istream
?
推荐答案
struct MyIStream : public istream {
MyIStream(MyIStream&& str) : istream(move(str)) {}
};
这不起作用,因为 basic_ios
是 istream
包含成员函数 void move(basic_ios& _Other)
(用于移动基类)。
This doesn't work because basic_ios
, a base class of istream
contains a member function void move(basic_ios& _Other)
(for moving the base class).
如果在构造函数中使用 std :: move(str)
,它会编译!
If you use std::move(str)
in the constructor, it compiles!
这篇关于移动从istream派生的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!