The <<= operator in ostrstream

Hi All,

I'm working on an old code and seen the following usage of the ostrstream class:

1
2
3
4
5
...
ostrstream s;
float f=1.234;
s <<= f;
...


I never seen such usage of the assignment by bitwise left shift operator.

Any idea what this segment suppose to do?
There is no operator <<= for std::ostrstream, so this fails to compile in standard C++.

In the past, <<= was occasionally overloaded by non-standard string libraries to generally mean "append to" (it lost to += by 1998). I haven't seen it overloaded for streams, but it possibly means the same. To learn what it does exactly, you'd have to examine the headers to find the definition of this operator in the codebase you're working on.
Thank you for your answer Cubbi,

I'm also suspecting that the usage was in non-standard C++ lib that the project used in the past, the thing that surprises me is the this operator DOES compile and run in standard compiler (I'm using gcc version 4.5.2) but the output of this operator seems like binary representation for some reason...
I went as far back as gcc 4.3.2:

$ cat test.cc
#include <iostream>
#include <strstream>
int main()
{
    std::ostrstream s;
    float f=1.234;
    s <<= f;

    std::cout << s.str() << '\n';
    s.freeze(false);
}


$ /opt/swt/bin/g++ --version | head -1
g++ (GCC) 4.3.2


$ /opt/swt/bin/g++ -Wno-deprecated -o test test.cc
test.cc: In function 'int main()':
test.cc:7: error: no match for 'operator<<=' in 's <<= f'


Do you have a standalone test?
Last edited on
Hi Cubbi,

You were right, on my side the stand-alone program doesn't work as well, after digging deep in the program code I found an include that overloads almost all primitive types in the ostrstream <<= operator, now I also know what this operator does, thank!
Topic archived. No new replies allowed.