Razzer wrote:
Recently, I saw someone use this marco:
#define MAKE_STRING(msg) \
(((ostringstream&)(ostringstream()<<msg)).str())
(Assume that the std namespace had been brought into the global scope.)
My question is that if this is defined. I think isn't. It seems to me
that the temporary is being passed around to non-const references
during its lifetime. Am I just plain wrong?
I really don't see a problem with that code.
Basically it's the same as if you write something like
int x = atoi( (std::string("1") + std::string("2")).c_str() );
which should be perfectly fine.
Temporaries created as a result of expression evaluation are
guaranteed to live exactly until the end of the expression
statement, which would be that ";" thingy.
Meanwhile it's perfectly fine to use them as an l-value like in:
int x = atoi( (std::string() = "10").c_str() );
or even
int foo = atoi( ((std::string() = "10") = "142").c_str() + 1 );
or like in the MAKE_STRING macro you posted.
I think what you mean is the nasty
std::string& foo = std::string("foo");
thingy which is of course bullshit (although it compiles).
But since the "str()" function (which is the "result" of that
MAKE_STRING macro you posted) finally returns a "basic_string"
and not a referenct to something that's all not a problem here.
And finally, the cast to "ostringstream&" is only there because
operator "<<" returns a ref to "basic_ostream" (base-type), but
the "str()" function is defined in "ostringstream" so you have
to cast it back to a ref to the "real" type to be able to call it.