Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

Saturday, 16 April 2011

Create string on the fly just in one line

We often make use of std::stringstream to convert values of various types into a single std::string. For example, see this:

std::stringstream ss;
ss << 25 << " is greater than " << 5;
std::string s = ss.str(); //s => "25 is greater than 5"

Paul (at stackoverflow) was trying to think of a clever way to concatenate various things into a single string without having to use an ostringstream explicitly. He came up with a macro which he didn't like though.