I was messing around with std::ostringstream , considering this question: sprintf in C ++? and noticed the packaging of stringbuilder() from Nawaz and thought it should work with std::ostringstream .
So my first attempt was as follows:
std::cout << (std::ostringstream("select * from foo limit") << max_limit).str() << std::endl;
Now this obviously does not compile (correctly), since the result of operator<< is std::ostream - which does not have str() member. Therefore, I thought that the actor should do the trick, and specifically, throw a const link on the link (also works with casting to a regular link), so the second attempt:
std::cout << static_cast<std::ostringstream const&>(std::ostringstream("select * from foo limit") << max_limit).str() << std::endl;
Now this compiles fine and works, however the output, well, is not what I expected.
10lect * from foo limit
Now - here is the question, do I cause some undefined behavior somewhere - and if so where? And how is this different from the approach that stringbuilder did (I suppose, in addition to the result of its operator, there is stringbuilder itself, and not std::ostream ).
EDIT: here is the ideone code .
EDIT: oops - forgot to specify, max_limit - int .
source share