error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ sl << ss;

error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ sl << ss;

我的朋友给我发了一个代码,他说在 Windows 中编译成功。我试过 linux 但它没有给出下面的错误。以下是代码的最小可验证示例。

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
   std::stringstream ss, sl;
   sl << ss;
}

但它给出了

error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’
    sl << ss;

为什么它在 windows 中有效但在 linux 中无效,为什么会出现此错误?

自 C++11 起,此代码无法编译,因为 operator<< 与两个 std::stringstream.

类型的操作数没有匹配的重载

但是,在 C++11 之前,std::ostream 提供了 implicit conversion to void *,因此可以调用以下重载:

basic_ostream& operator<<( const void* value );

如果流有错误,输出将与输出空指针相同,否则一些未指定的非空指针值。

可能您的朋友使用了旧的编译器,或者编译器运行处于旧的兼容模式。