This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Debug class optimization
- From: Mad Hatter <slokus at yahoo dot com>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 30 Dec 2003 12:07:58 -0800 (PST)
- Subject: 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/