Quote:Original post by snk_kid
This is the first time i've come across that thread and you know all of those casts are unnecessary and pretty wrong, do you understand whats going on with that code? do you know why Sharlin's solution doesn't work, i can tell you if you want, in any case the correct code is just:
#define mkstr(args) static_cast<std::ostringstream&>(const_cast<std::ostringstream&>(std::ostringstream()) << args).str()
GCC isn't satisfied with that:
error: invalid const_cast of an rvalue of type ` std::ostringstream' to type `std::ostringstream&'
It needs a static_cast<const std::ostringstream &> around the constructor like so:
#define mkstr(args) static_cast<std::ostringstream&>( const_cast<std::ostringstream&>(static_cast<const std::ostringstream &>(std::ostringstream())) << args ).str()
and it works.
It's still cleaner than the original with the redundand upcast. Thanks for bringing this up again. Looks like back then I just settled for the first thing that worked.[smile]
What I'd still like to know is why doesn't this work (it prints the address of the first string constant, as discussed in the
other thread):
std::cout << (static_cast<std::ostringstream&>(std::ostringstream() << "A" << 4)).str() << std::endl;