By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Enabling and disabling a macro

P: n/a
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?
Dec 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Giannis Papadopoulos wrote:
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.

Dec 17 '05 #2

P: n/a
In article <dn**********@newsmaster.pub.dc.hol.net>,
Giannis Papadopoulos <ip******@inf.uth.gr> wrote:
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);
Dec 17 '05 #3

P: n/a
Christian Bau wrote:
In article <dn**********@newsmaster.pub.dc.hol.net>,
Giannis Papadopoulos <ip******@inf.uth.gr> wrote:
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);


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
Dec 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.