debug class

Mad Hatter
Thu Dec 18 03:34:00 GMT 2003


I'm trying to write a small debug class that will output debug
messages if DEBUG is true and suppress them completely if false. Though
the output is suppressed as expected, I find (by looking at the
generated assembler file) that gcc is not eliding the calls
completely when DEBUG is false even though they do nothing.

Can someone comment on whether this is
a limitation of gcc or a language requirement ? Thanks. This is what my
code looks like:
#include <iostream>
#include <fstream>

// define DEBUG on command line to be 'true' or 'false'

template<bool d> struct Dbg {
    Dbg( std::ostream & ) { }

// default -- do nothing
template< bool b, typename T > Dbg<b>
operator<<( Dbg<b> aDebug, T const )
    return aDebug;

// specialize Dbg and operator<< for 'true'
template<> struct Dbg<true> {
    std::ostream &os;
    Dbg( std::ostream &aOs ) : os( aOs ) { }
typedef Dbg<true> dbg_true_t;

template< typename T > Dbg<true>
operator<<( Dbg<true> aDebug, T const &a )
    aDebug.os << a;
    return aDebug;

Dbg<DEBUG> debug( std::cerr );  // our debug object

    debug << 3;  // disappears if DEBUG is false

Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.

More information about the Gcc-help mailing list