简体   繁体   English

为什么编译器现在接受从 std::stringstream::operator<<() 返回的 std::ostream& 上调用 str() 成员?

[英]Why do compilers now accept to call str() member on a returned std::ostream& from std::stringstream::operator<<()?

Consider the following line:考虑以下行:

std::string s = (std::stringstream() << "foo").str();

This should not compile because std::stringstream::operator<<() is inherited by std::ostream and returns a std::ostream& which does not have an str() member.这不应该编译,因为std::stringstream::operator<<()std::ostream继承并返回一个没有str()成员的std::ostream&

It seems the main compilers are now accepting this code where they didn't in the past.似乎主要的编译器现在接受了过去不接受的代码。 I would like to understand what standard change happened to make this to compile?我想了解为了编译而发生了什么标准变化?

I made some tests with gcc , clang and msvc and I could find the version where the change happened:我用gccclangmsvc做了一些测试,我可以找到发生变化的版本:

Compiler编译器 Rejects until (version)拒绝直到(版本) Accepts from (version)接受来自(版本)
GCC GCC 11.1 11.1 11.2 11.2
Clang Clang 12.0.1 12.0.1 13.0.0 13.0.0
MSVC MSVC v19.14 v19.14 v19.15 v19.15

You can find the test here你可以在这里找到测试

They all added the rvalue overload (see here ) very late.他们都添加了右值重载(见这里)很晚。

The rvalue overload was introduced in C++11 and returns the same type of stream as its left-hand operand.右值重载在 C++11 中引入,并返回相同类型的 stream 作为其左操作数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;运算符&lt;&lt;(std :: ostream&,int&)&#39;不明确 - 'operator<<(std::ostream&, int&)’ is ambiguous std :: ostream&运算符&lt;&lt;类型推导 - std::ostream& operator<< type deduction 没有已知的从std :: ostream *到std :: ostream&的转换 - No known conversion from std::ostream* to std::ostream& “friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, LinkedList&amp; list)”是什么意思? - What does “friend std::ostream& operator<<(std::ostream& out, LinkedList& list)” mean? std :: ostream&运算符&lt;&lt;(std :: ostream&sstr,const T&val)的模棱两可的重载 - Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val) std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) 未被覆盖 - std::ostream& operator<<(std::ostream&, const T&) not being overridden 重载运算符std :: ostream&operator &lt;&lt;打印实例内存地址 - Overloaded operator std::ostream& operator<< prints instance memory address 如何在std:ostream&member中正确使用,而“this”是const? - how to use use properly in std:ostream& member, while “this” is a const? 重载&lt;&lt;在C ++中的运算符-为什么返回类型是引用std :: ostream& - Overloading the << operator in C++ -why return type is a reference std::ostream& 为什么必须在 C++ 的“全局”scope 中声明 `std::ostream&amp; operator&lt;&lt;` 覆盖? - Why `std::ostream& operator<<` overriding must be declare in 'global' scope in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM