Thread: Testing the value of an object-like macro

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    Testing the value of an object-like macro

    I see in some header files, such as Sun's <sys/feature_tests.h>, the value of a macro is subtracted by zero before testing it against a value. Example:
    Code:
    #if   (_XOPEN_SOURCE - 0 == 600) || (_POSIX_C_SOURCE - 0 == 200112L)
    #define _XPG6  
    #undef  _POSIX_C_SOURCE
    #define _POSIX_C_SOURCE                 200112L
    #endif
    Why didn't they just write
    #if (_XOPEN_SOURCE == 600) || (_POSIX_C_SOURCE == 200112L) ?
    Should we follow their style?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    because if _XOPEN_SOURCE is defined as empty string in their case you get

    (-0 == 600) - valid expression

    in your case

    ( == 600) - invalid expression
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    98
    In that case, it would also achieve the same purpose if I write
    #if (_XOPEN_SOURCE + 0 == 600)
    , right?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hzmonte View Post
    In that case, it would also achieve the same purpose if I write
    #if (_XOPEN_SOURCE + 0 == 600)
    , right?
    I would have thought so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Texture Management in OpenGL
    By Brafil in forum Game Programming
    Replies: 13
    Last Post: 07-16-2009, 04:32 PM
  3. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM