Listing I
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
 
 
// ... needed for debug
template< class type>
inline std::string to_string( const type & value)
{
    std::ostringstream streamOut;
    streamOut << value;
    return streamOut.str();
}
 
 
 
 
/////////////////////////////////////
// the 'str_stream' class
#ifdef NDEBUG
// Release mode
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;
};
 
 
#else
 
 
// Debug mode
struct str_stream
{
    operator std::string() const
    {
        return m_str;
    }
    mutable std::string m_str;
};
 
 
#endif
 
 
 
 
template< class type>
inline const str_stream & operator<< ( const str_stream & out, const type & value)
{
#ifdef NDEBUG
    out.underlying_stream() << value;
#else
    out.m_str += to_string( value);
#endif
    return out;
}
 
 
 
 
 
 
int main()
{
    int nWordsCount;
    // ... calculate words count
    nWordsCount = 48;
    // this works
    std::string str = str_stream() << "We have " << nWordsCount << " words";
 
 
    // this DOES NOT work AS EXPECTED
    //
    // you would expect '0035', but get '35' in Debug Mode (wrong)
    // and '0035' in Release (correct);
    //
    // this would mean different behavior in Debug/ Release,
    // which you certainly don't want
    std::string str2 = str_stream() << std::setfill( '0') << std::setw( 4) << 35;
    std::cout << str2;
    std::cin.get();
    return 0;
}