1

My friend sent me a code where he says sucessfuly compiled in Windows. I tried on linux and it failed giving the error below. Below is a minimum verifiable example of the code.

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

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

but it gives

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

Why it works in windows but not in linux, and why this error happens?

M.M
  • 138,810
  • 21
  • 208
  • 365
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • 1
    [Doesn't compile](https://rextester.com/TWCZY54033) with Visual C++ either. `sl << ss` part makes no sense. – Igor Tandetnik Apr 11 '20 at 22:49
  • 1
    The code, like you present it, shouldn't work anywhere. – anastaciu Apr 11 '20 at 22:50
  • Here's an old question that might help if you actually want to [concatenate stringstreams in c++](https://stackoverflow.com/q/8231746/243245) – Rup Apr 11 '20 at 22:53
  • If it actually does work for your friend, it'll be because the compilers have different versions of the STL and that one accepts this syntax. Can you find out which compiler they're using on Windows? – Rup Apr 11 '20 at 22:57

1 Answers1

3

Since C++11 this code fails to compile because there is no matching overload for operator<< with both operands of type std::stringstream.

However, prior to C++11, std::ostream provided implicit conversion to void *, so the following overload could be called:

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

The output would be the same as outputting a null pointer if the stream has an error, otherwise some unspecified non-null pointer value.

Probably your friend used an old compiler, or a compiler running in old compatibility mode .

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Thanks. Indeed now I recall it sais that in lots of blue lines but then the red line said `cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’`. The blue lines were the supported operators. But it wasn't clear by the red error that the error ocurred because there were no operators. – Guerlando OCs Apr 11 '20 at 23:26