Listing D |
#include <string> #include <sstream> struct as_string {}; inline std::string operator<< ( std::ostream & streamOut, const as_string &) { std::ostringstream & s = ( std::ostringstream &)streamOut; return s.str(); } int main() { int nWordsCount; // ... calculate words count nWordsCount = 48; std::string str = // construct a temporary stringstream and use it; // the as_string() will convert all to a string std::ostringstream() << "We have " << nWordsCount << " words" << as_string(); return 0; } |