c++ - 为什么将 `istream&` 用于临时的 `stringstream` 工作,但在使用 `stringstream&` 时却不行?
问题描述
考虑以下代码,它编译并运行:
#include <iostream>
#include <sstream>
struct Foo {};
void operator>>(std::istream &, Foo) {
}
int main() {
std::stringstream{} >> Foo{};
}
但是,如果我更改std::istream
为std::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) {
| ^~~~~~~~
c.cpp:4:6: note: conversion of argument 1 would be ill-formed:
c.cpp:7:10: error: cannot bind non-const lvalue reference of type 'std::stringstream&' {aka 'std::__cxx11::basic_stringstream<char>&'} to an rvalue of type 'std::stringstream' {aka 'std::__cxx11::basic_stringstream<char>'}
7 | std::stringstream{} >> Foo{};
| ^~~~~~~~~~~~~~
这是有道理的:我不能将左值引用绑定到右值(临时)对象。
为什么第一个代码会编译?
UPD:我的编译器是
g++ (Rev2, Built by MSYS2 project) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
和标志是-std=gnu++17 -Wall -Wextra -Wshadow -O2
UPD2:最后,Brian Bi 所说的操作员有一个错误:
In file included from C:/Software/msys64/mingw64/include/c++/10.3.0/iostream:40,
from c.cpp:1:
C:/Software/msys64/mingw64/include/c++/10.3.0/istream:980:5: note: candidate: 'template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&)'
980 | operator>>(_Istream&& __is, _Tp&& __x)
| ^~~~~~~~
C:/Software/msys64/mingw64/include/c++/10.3.0/istream:980:5: note: template argument deduction/substitution failed:
C:/Software/msys64/mingw64/include/c++/10.3.0/istream: In substitution of 'template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::__cxx11::basic_stringstream<char>; _Tp = Foo]':
c.cpp:7:32: required from here
C:/Software/msys64/mingw64/include/c++/10.3.0/istream:980:5: error: no type named 'type' in 'struct std::enable_if<false, std::basic_istream<char>&>'
解决方案
尽管看起来很奇怪,但这段代码格式正确。它应该始终编译。见神箭。operator>>
当重载更改为 take时,它也应该编译std::stringstream&
。
operator>>
原因是从 派生的类存在特殊的右值重载,在此处std::ios_base
标记为 (3) :
template< class Istream, class T >
Istream&& operator>>( Istream&& st, T&& value );
效果相当于:
st >> std::forward<T>(value);
return std::move(st);
这是operator>>
您的代码调用的重载。它委托给operator>>
你写的。(由于Foo
是全局命名空间的成员,非限定名称查找将在operator>>
任何上下文中找到您,这要归功于 ADL。)
如果您的工具链没有右值流提取运算符,那么您的代码将无法编译,因为很明显,非常量左值引用 ,Base&
永远不会绑定到类型的右值Derived
(其中Derived
派生自Base
)。
我不知道为什么标准库中存在右值流提取运算符。我认为这是因为有人意识到,如果碰巧是流类型的右值表达式,没有充分的理由is >> x
不工作。is
但是许多现有operator>>
的 s 都是以左值引用basic_istream
作为第一个参数的自由函数。所以添加这个右值operator>>
重载,它代表一个左值,是解决这个问题的方法。根据这种观点,您的代码编译的事实不是错误:您可以编写一个operator>>
对流类型采用非常量左值引用并使其也适用于右值的函数,这是有意的。
推荐阅读
- javascript - 使用 for loop 和 map 函数使用 hsl 和 javascript 制作动态彩色圆圈
- shell - 我需要使用 shell 脚本创建一个包含所有 hashcorp 保险库键值对数据的文件
- mysql - 在不使用索引的情况下防止插入重复项
- php - 循环检查多维数组 - PHP
- postgresql - 如何在 PostgreSQL 中仅显示没有子分区的表列表
- c++ - 给定一个类 foo,为什么 foo::foo::foo::foo::method() 工作?
- javascript - 正则表达式:查找函数第一个参数上使用的属性
- react-native - React Native Firebase Social Auth 在发布时不起作用
- javascript - 如何从随机化数组输出的函数中排除输出?
- python - 从网络下载文件到烧瓶服务器