CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2002
    Posts
    72

    A question about #define

    Hi, everyone!

    Please look at the following statement,

    --------
    #define assert(x) ((void)0)
    --------

    What does the statement mean? What is the function
    of "((void)0)" here?

    Another question, there is a paramater "x" in the former
    "assert(x)", why there is not a parameter in the latter
    "((void)0)"?

    Cheers,
    George

  2. #2
    Join Date
    May 2001
    Posts
    472
    Sometimes you want a macro such as ASSERT to amount to nothing in specific builds, so you write code like this:
    Code:
    #ifdef  _DEBUG
    	#define CHECK(f, message) \
    		if (!(f)) \
    		{ \
    			int ID = AfxMessageBox(__FILE__ ":\n" _T(message), MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_DEFBUTTON3); \
    			if (ID == IDABORT) \
    				exit(0); \
    			else if (ID == IDRETRY) \
    				AfxDebugBreak(); \
    		}
    #else // _DEBUG
    	#define CHECK(f, message) ((void)0)
    #endif// _DEBUG
    Ce n'est que pour vous dire ce que je vous dis.

  3. #3
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    What does the statement mean? What is the function
    of "((void)0)" here?
    It means do nothing. The alternative is

    #define assert(x)

    This may confuse some people but it defines assert(x) as nothing.
    Another question, there is a paramater "x" in the former
    "assert(x)", why there is not a parameter in the latter
    "((void)0)"?
    x is not used but is put in place to keep the compiler happy. assert(x) is normally used in to check whether x is non zero. If it is zero, the program will stop. This macro nullifies the effect of assert.
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured