#include <string> #include <sstream> struct str_stream { std::stringstream & underlying_stream() const { return m_streamOut; } operator std::string() const { return m_streamOut.str(); } private: mutable std::stringstream m_streamOut; }; template< class type> const str_stream & operator<< ( const str_stream & out, const type & value) { out.underlying_stream() << value; return out; } int main() { int nWordsCount; // ... calculate words count nWordsCount = 48; std::string str = str_stream() << "We have " << nWordsCount << " words"; return 0; } |