Text removing using a macro in a namespace

M

marco_segurini

Hi,

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'?

/////////////////////////
//#define DBG

namespace MY_NAMESPACE
{
#if defined(DBG)
void MY_DUMP(int)
{
}
#else // release case
int pippo;
#define MY_DUMP(a) pippo
#endif
}

int main()
{
MY_NAMESPACE::MY_DUMP(1); // line 18
return 0;
}
////////////////////////

Thanks a lot.
Marco.
 
J

Jonathan Turkanis

marco_segurini said:
Hi,

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'?

/////////////////////////
//#define DBG

namespace MY_NAMESPACE
{
#if defined(DBG)
void MY_DUMP(int)
{
}
#else // release case
int pippo;
#define MY_DUMP(a) pippo
#endif
}

int main()
{
MY_NAMESPACE::MY_DUMP(1); // line 18
return 0;
}


Macros don't know anything about namespaces. Take a look at the preprocessed
output.

If DBG is not defined, you get:

namespace MY_NAMESPACE
{
int pippo;
}

int main()
{
MY_NAMESPACE::pippo;
return 0;
}

Otherwise, you get:

namespace MY_NAMESPACE
{
void MY_DUMP(int)
{
}
}

int main()
{
MY_NAMESPACE::MY_DUMP(1);
return 0;
}

It's hard for me to see why you would want either. What is your goal?

Jonathan
 
L

Larry Brasfield

marco_segurini said:
Hi,

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'?

Most compilers would not. But there is a much clearer way
to say "do this" for one build and "do nothing" for another.
See insertions below. (I assume 'pippo' had no real purpose.)
/////////////////////////
//#define DBG

namespace MY_NAMESPACE
{
#if defined(DBG)
void MY_DUMP(int)
{
}
#else // release case #if 0
int pippo;
#define MY_DUMP(a) pippo
#else
#define MY_DUMP(a)
#endif
#endif
}

int main()
{
MY_NAMESPACE::MY_DUMP(1); // line 18
return 0;
}
////////////////////////

Thanks a lot.
Good luck.
 
M

marco_segurini

Thanks Jonathan Turkanis,
It's hard for me to see why you would want either. What is your goal?

My goal is to call a free-function defined in a namespace ONLY if DBG
is defined. I don't want to wrap all the free-function calls with {
#if DBG, #endif } pair.

Marco.
 
M

marco_segurini

Thanks Larry Brasfield,

If I apply your modification with DBG non defined the preprocessor output is:

namespace MY_NAMESPACE
{
}

int main()
{
MY_NAMESPACE::; <<<<<<<<<<<<
return 0;
}

that is the source of all my problems.

Marco.
 
J

Jonathan Turkanis

marco_segurini said:
Thanks Jonathan Turkanis,


My goal is to call a free-function defined in a namespace ONLY if DBG
is defined.

I see.
I don't want to wrap all the free-function calls with {
#if DBG, #endif } pair.

I don't blame you. Lot's of #ifdef's can be ugly.

Try:

#ifdef DBG
# define DEBUG_DUMP(x) my_namespace::debug_dump(x);
#else
# define DEBUG_DUMP(x)
#endif

Jonathan
 
D

Donovan Hawkins

marco_segurini said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top