This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

debug class


Hi,

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

int
main()
{
    debug << 3;  // disappears if DEBUG is false
}
--------------------------------------------------------

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]