gpt4 book ai didi

c++ - 为什么将 `istream&` 取到临时 `stringstream` 工作,但取 `stringstream&` 时不起作用?

转载 作者:行者123 更新时间:2023-12-04 12:13:54 27 4
gpt4 key购买 nike

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

#include <iostream>
#include <sstream>
struct Foo {};
void operator>>(std::istream &, Foo) {
}
int main() {
std::stringstream{} >> Foo{};
}
但是,如果我更改 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) {
| ^~~~~~~~
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{};
| ^~~~~~~~~~~~~~
这是有道理的:我不能将左值引用绑定(bind)到右值(临时)对象。
为什么第一个代码编译?
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>&>'

最佳答案

尽管看起来很奇怪,但这段代码格式良好。它应该总是编译。见 Godbolt .它也应该在 operator>> 时编译重载改为取std::stringstream& .
原因是存在一个特殊的右值operator>>std::ios_base 派生的类的重载, 标记 (3) here :

template< class Istream, class T >
Istream&& operator>>( Istream&& st, T&& value );
效果相当于:
st >> std::forward<T>(value);
return std::move(st);
这是 operator>>由您的代码调用的重载。它委托(delegate)给 operator>>你写的。 (因为 Foo 是全局命名空间的成员,由于 ADL,不合格的名称查找将在任何上下文中找到您的 operator>>。)
如果您的工具链没有右值流提取运算符,那么您的代码将无法编译,因为很明显,这是一个非常量左值引用, Base& , 永远不会绑定(bind)到 Derived 类型的右值(其中 Derived 来自 Base )。
我不知道为什么标准库中存在右值流提取运算符。我认为这是因为有人意识到 is >> x 没有充分的理由不工作,如果 is恰好是流类型的右值表达式。但是很多现有的 operator>> s 是自由函数,采用对 basic_istream 的左值引用作为他们的第一个论点。所以添加这个右值 operator>>委托(delegate)给左值的重载是这个问题的解决方案。根据这个观点,你的代码编译的事实不是一个错误:你可以故意写一个 operator>>它采用对流类型的非常量左值引用,并使其也适用于右值。

关于c++ - 为什么将 `istream&` 取到临时 `stringstream` 工作,但取 `stringstream&` 时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69320918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com