#include <sstream>#include <iostream>template < bool Enabled >class Appender{public: Appender( void ) {} ~Appender( void ) { std::cout << buf.str() << std::endl; //OutputDebugString( buf.str().c_str() ); } template < class T > inline Appender<Enabled> & operator<<( const T &t ) { buf << t; return *this; }private: std::ostringstream buf;};template <>class Appender<false>{public: template < class T > Appender<false> & operator<<( const T &t ) { return *this; }};#ifdef _DEBUGtypedef Appender<true> DebugOut;#elsetypedef Appender<false> DebugOut;#endifint main(int argc, char* argv[]){ int x = 10; DebugOut() << x << "Hello" << 15 << 'a'; std::cout << "--------" << std::endl; DebugOut() << x << "Hello" << 15 << 'a';}
This doesn't generate code in MVS release build, with possible exception of functions that would get called as parameters for the output (even there, they might not get compiled, since compiler is smart enough to figure out that some actions don't have side-effects).