debug class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux