【发布时间】:2018-11-03 01:08:12
【问题描述】:
我有一个围绕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
我可能会构建它,但由于我没有看到任何>> &&,所以感觉有些不对劲。
>>&& 在某种程度上是被禁止的还是邪恶的?
【问题讨论】:
-
不相关,
os >> ......- 很确定你的意思是os << ......。 -
你为什么要通过电话发布非常技术性的问题?
-
这是用什么工具链?我问是因为,除非我没有看到眼前的东西,clang has no problems with this。
-
@Michał 是的,我是那个打勾的。