Consider the following code:
#include <sstream>
#include <iostream>
class Foo : public std::stringstream {
public:
~Foo() { std::cout << str(); }
};
int main()
{
Foo foo;
foo << "Test1" << std::endl;
Foo() << "Test2" << std::endl;
return 0;
}
When I execute this, it gives me:
004177FC
Test1
I do not understand why the second example gives me gibberish. The temporary should live until the entire expression is evaluated, so why does it not behave the same as the first example?
I tested it.
I can guess that
operator<<
cannot bind a temporary to a non-const reference, so any externally defined operator<< functions will not work on the Foo temporary, but any class member ones will so ifostream
orostringstream
has any internaloperator<<
members they will work.Therefore it may be that the overload to a pointer is a member function whilst the special one for const char * is externally declared.
The non-temporary can bind to the non-const reference for the more specialist overload.
If you really need this you can workaround with a wrapper
Tested and works. The first operator<< will return you the underlying stream.
I tried this too but it coredumped:
This also works: