"marco_segurini" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
I like to know if this is a good way to remove 'line 18' from
compilation when DBG is not defined (I dislike to wrap 'line 18' using
#if/#endif pair) of if there are better ways (always using the
namespace).
Am I sure that the compiler generates non code for 'line 18'?
I personally would use the following (I modified your namespace to use mixed
case since you seem to use all upper for macros like many people):
namespace MyNamespace
{
inline void MyDump( int n )
{
#if defined DBG
// Code that handles dump
#endif
}
};
While it is true that you cannot know for certain that the compiler will
optimize this away, I would still say use this unless you know for certain
you cannot afford to. I write real-time embedded software and even I would
do it this way. Basically, my opinion is if the compiler doesn't perform
this trivial optimization then it didn't perform a million other
optimizations I wanted so that this one inefficiency will be insignificant.
If you insist on a macro I would suggest the following...it is a common
method used in assert macros. A constant will gobble up the semicolon as
well as a variable, and the void makes sure you can't do anything with it
(in other words, it simulates the void returned from the debug function so
they are usable in the same ways).
namespace MyNamespace
{
#if defined DBG
inline void MyDump( int n )
{
// Code that handles dump
}
#else
#define MyDump( n ) ( (void) 0 )
#endif
};
Donovan Hawkins