[英]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:我用gcc 、 clang和msvc做了一些测试,我可以找到发生变化的版本:
Compiler![]() |
Rejects until (version)![]() |
Accepts from (version)![]() |
---|---|---|
GCC ![]() |
11.1 ![]() |
11.2 ![]() |
Clang ![]() |
12.0.1 ![]() |
13.0.0 ![]() |
MSVC ![]() |
v19.14 ![]() |
v19.15 ![]() |
You can find the test here你可以在这里找到测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.