This is the mail archive of the gcc@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 optimization


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 when DEBUG is false,
I find that the generated assembler file still seems to have
the calls even though they do nothing.

Can someone comment on whether this is a limitation of gcc
or a language requirement that the calls must not be elided ?
I'm using gcc version 3.2.2 on Mandrake Linux 9.1 with the
following command line options:
    g++ -O2 -S -ansi -pedantic -Wall -W 

Thanks.

This is what my code looks like:

-----------------------------------------------------------------
#include <iostream>
#include <fstream>

// change DEBUG to 'false' to suppress output
#define DEBUG true

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 ) { }
};

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;  // output suppressed 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]