为什么istream不支持右值提取

Why doesnt istream support rvalue extraction

本文关键字:提取 不支持 istream 为什么      更新时间:2023-10-16

我有一个类,它封装在std::string周围以提供格式:

struct Wrap {
std::string& s; // need const ref for output, non const for input 
friend std::ostream& operator<< (std::ostream& os, const Wrap& w) {
os << "[" << w.s << "]";
return os;
}
friend std::istream& operator>> (std::istream& is, Wrap&& w) {
Is >> ......;
return is;
}
};

输出也可以:

my_ostream << Wrap{some_string};

因为将temp Wrap绑定到const ref是可以的。

但输入不太好:

my_istream >> Wrap{some_string}; // doesn't compile - cannot bind lvalue to rvalue

我可能构建了它,但由于我没有看到任何>> &&,所以感觉有些不对劲。

>>&&在某种程度上是被禁止的还是邪恶的?

(在gcc 7.3.0版本(Ubuntu 7.3.0-16ubuntu3(上测试(

您的代码可以按原样运行(在此处运行:http://cpp.sh/9tk5k):

#include <string>
#include <iostream>

struct Wrap {
std::string& s; // need const ref for output, non const for input 
friend std::ostream& operator<< (std::ostream& os, const Wrap& w) {
os << "[" << w.s << "]";
return os;
}
friend std::istream& operator>> (std::istream& is, Wrap&& w) {
is >> w.s;
return is;
}
};

int main() {
std::string a = "abcd";
std::cin >> Wrap{a};
std::cout << Wrap{a};
}

您应该能够将Wrap作为r值传递。如果你是在线创建的,这正是发生的事情。

将r值与const-ref绑定也应该(而且确实(起作用。

右值引用只能绑定到右值。大多数情况下,这就是你想要的——它确保(例如(当你编写一个移动ctor/赋值运算符时,你不会意外地在左值上调用它,并破坏仍将使用的东西。

我不知道你为什么要在这种情况下使用右值引用,但你确实需要它是有原因的,当它是模板参数时,你至少可以使用相同的语法:

struct Wrap
{
std::string s; // need const ref for output, non const for input
friend std::ostream &operator<<(std::ostream &os, const Wrap &w)
{
os << "[" << w.s << "]";
return os;
}
template <class T>
friend std::istream &operator>>(std::istream &is, T &&w)
{
is >> w.s;
return is;
}
};
int main() {
int x;
Wrap w;
std::cin >> w;
}

但不确定这是否真的有用。