'std :: basic_ostream< char>'中没有名为'str'的成员与gcc和clang,但与msvc没问题 [英] no member named 'str' in 'std::basic_ostream<char>' with gcc and clang, but no problem with msvc
问题描述
此代码段( https://gcc.godbolt.org/z/hKDMxm ):
#include <iostream>
#include <sstream>
using namespace std;
int main() {
auto s = (ostringstream{} << "string").str();
cout << s;
return 0;
}
可使用msvc编译并按预期运行,但无法使用clang 9.0.0和gcc 9.2进行编译,并显示以下错误消息:在'std :: basic_ostream< char>'中没有名为'str'的成员
.查看 https://en.cppreference.com/w/cpp/io/basic_ostringstream/str 显然是 ostringstream
的 str()
成员.为什么clang和gcc无法编译此代码?
compiles and runs as expected with msvc, but fails to compile with clang 9.0.0 and gcc 9.2 giving this error message:no member named 'str' in 'std::basic_ostream<char>'
. Looking at https://en.cppreference.com/w/cpp/io/basic_ostringstream/str there is clearly str()
member of ostringstream
. Why clang and gcc are failing to compile this code?
推荐答案
显然是
ostringstream
是的,但是根据 cppreference 这种<<
应该返回对 basic_ostream< ...>
的引用,而不是对 ostringstream
的引用.
Yes, but according to cppreference this overload of <<
should return a reference to basic_ostream<...>
rather than ostringstream
.
libstdc ++(GCC的标准库)完全做到了这一点,而libc ++(Clang的标准库)和MSVC的标准库在技术上在这里表现不正确.
libstdc++ (GCC's standard library) does exactly this, while libc++ (Clang's standard library) and MSVC's standard library behave incorrectly here, technically.
但是,似乎有一个打开的缺陷报告提示与右值流一起使用的<<
的重载应返回传递给它的确切流类型.如果它被接受,您的代码将是有效的.
However, it seems there is an open defect report suggesting that the overload of <<
that works with rvalue streams should return the exact stream type that was passed to it. If it gets accepted, your code will be valid.
这篇关于'std :: basic_ostream< char>'中没有名为'str'的成员与gcc和clang,但与msvc没问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!