相关疑难解决方法(0)

为什么将“istream&”放入临时“stringstream”可以工作,但在使用“stringstream&”时却不起作用?

考虑以下代码,它编译并运行:

#include <iostream>
#include <sstream>
struct Foo {};
void operator>>(std::istream &, Foo) {
}
int main() {
    std::stringstream{} >> Foo{};
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我更改std::istreamstd::stringstream,则会出现错误:

c.cpp: In function 'int main()':
c.cpp:7:25: error: no match for 'operator>>' (operand types are 'std::stringstream' {aka 'std::__cxx11::basic_stringstream<char>'} and 'Foo')
    7 |     std::stringstream{} >> Foo{};
      |          ~~~~~~~~~~~~~~ ^~ ~~~~~
      |          |                 |
      |          |                 Foo
      |          std::stringstream {aka std::__cxx11::basic_stringstream<char>}
c.cpp:4:6: note: candidate: 'void operator>>(std::stringstream&, Foo)' (near match)
    4 | void operator>>(std::stringstream &, Foo) {
      | …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue

7
推荐指数
1
解决办法
330
查看次数

标签 统计

c++ ×1

rvalue ×1