Enabling and disabling a macro

  • Thread starter Giannis Papadopoulos
  • Start date
G

Giannis Papadopoulos

I want to create a macro that it must be ignored when NDEBUG is defined.

The macro I've created is

#ifndef NDEBUG
# define ERROR_MESSAGE(s) fprintf(stderr, "%s(): %s\n", __func__, (s));
#else
# define ERROR_MESSAGE(s)
#endif

However, when I checked assert.h to see how the assert() macro was
implement, I ran across

#if defined __cplusplus && __GNUC_PREREQ (2,95)
# define __ASSERT_VOID_CAST static_cast<void>
#else
# define __ASSERT_VOID_CAST (void)
#endif

#ifdef NDEBUG
# define assert(expr) (__ASSERT_VOID_CAST (0))
#else /* Not NDEBUG. */
/* some code */
#endif


Is there any could reason making my macro ((void)0) when I disable it?
 
J

Jordan Abel

Giannis said:
I want to create a macro that it must be ignored when NDEBUG is defined.

The macro I've created is

#ifndef NDEBUG
# define ERROR_MESSAGE(s) fprintf(stderr, "%s(): %s\n", __func__, (s));
#else
# define ERROR_MESSAGE(s)
#endif

However, when I checked assert.h to see how the assert() macro was
implement, I ran across

# define __ASSERT_VOID_CAST (void) ....
# define assert(expr) (__ASSERT_VOID_CAST (0)) ....


Is there any could reason making my macro ((void)0) when I disable it?

not really. shouldn't hurt anything.

There is a specific expansion list, of six preprocessor tokens,
required for assert() when disabled by the standard - that is ( ( void
) 0 ). You are not required to follow that for your own macros.
 
C

Christian Bau

Giannis Papadopoulos said:
I want to create a macro that it must be ignored when NDEBUG is defined.

The macro I've created is

#ifndef NDEBUG
# define ERROR_MESSAGE(s) fprintf(stderr, "%s(): %s\n", __func__, (s));
#else
# define ERROR_MESSAGE(s)
#endif

However, when I checked assert.h to see how the assert() macro was
implement, I ran across

#if defined __cplusplus && __GNUC_PREREQ (2,95)
# define __ASSERT_VOID_CAST static_cast<void>
#else
# define __ASSERT_VOID_CAST (void)
#endif

#ifdef NDEBUG
# define assert(expr) (__ASSERT_VOID_CAST (0))
#else /* Not NDEBUG. */
/* some code */
#endif


Is there any could reason making my macro ((void)0) when I disable it?

First try to compile this using your macros:

if (n == 0)
ERROR_MESSAGE ("n should not be zero");
else
call_function_using_n (n);
 
G

Giannis Papadopoulos

Christian said:
First try to compile this using your macros:

if (n == 0)
ERROR_MESSAGE ("n should not be zero");
else
call_function_using_n (n);

Yes, you are right, there is an excess ';' at the end of fprintf. And
because I'm always using brackets, I didn't notice it.

Damn copy-paste.. Tnx Christian..

--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.users.uth.gr
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top