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

    Angry Are #defines evil?

    I tried to compile a class with:
    static const int ERROR = 0;

    seems harmless enough, but I get an error:
    error C2059: syntax error : 'constant'

    on digging deeper, I see the following definition in :
    c:\program files\microsoft visual studio 8\vc\platformsdk\include\wingdi.h

    #define ERROR 0

    If I rename my static data member to MY_ERROR, then the problem goes away?

    Am I to conclude that all the # defines in the various windows headers have polluted the namespace? What gives?

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Are #defines evil?

    Well possibly your compiler can't handle it, this issue has been verified with MSVC. So do this:

    In your class header:

    Code:
    static const int ERROR;
    Then in the top of your class implementation file or source file:

    Code:
    const int MyClass::ERROR=0;
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Are #defines evil?

    Quote Originally Posted by daz001
    Am I to conclude that all the # defines in the various windows headers have polluted the namespace?
    Macros do not obey the rules of scope.

    You might want to read Stroustrup's FAQ on So, what's wrong with using macros? Unfortunately, it will not help you in this case, except maybe to suggest that you use a different naming convention for non-macro names.

    Quote Originally Posted by ahoodin
    Well possibly your compiler can't handle it, this issue has been verified with MSVC.
    The version of MSVC is recent enough that initialising integral class constants in the class definition should not pose a problem. It looks like the problem is clearly due to a conflict with a macro name, in which case your solution would also not work.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    May 2009
    Posts
    3

    Re: Are #defines evil?

    Thanks,

    I think it will be a good idea to never use tokens with all caps. Maybe "Error" would have been much better here.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Are #defines evil?

    Quote Originally Posted by daz001
    I think it will be a good idea to never use tokens with all caps.
    Except when it is a macro name, and if you use a header inclusion guard you would certainly be defining a macro.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: Are #defines evil?

    As Meyers wrote:

    "Given the availability of consts, enums and inlines, your need for the preprocessor (especially #define) is reduced. bit it's not eliminated.
    #include remains essential, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the pre-processor, but you should definitely give it long and frequent vacations."

    Doron

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Are #defines evil?

    Are #defines evil?
    Of course! I blame them for the current world economic turmoil, as well as the basis of many conspiracy theories.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Are #defines evil?

    ahoodin asked why I wrote that his/her solution would not work. My reasoning is that if a header is included such that this macro definition is in effect:
    Code:
    #define ERROR 0
    Then the declaration:
    Code:
    static const int ERROR;
    would become:
    Code:
    static const int 0;
    after preprocessing, and this would result in a compile error. This is pretty much the problem that daz001 was asking about.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Are #defines evil?

    No, I didn't ask. Must have been a miss-communication. Good answers though Laserlight.

    Are #defines evil?
    I dunno about evil, maybe naughty. They are naughty bits left over from C.

    The worst part of #defines are that mostly they elude the compiler. The doesn't complain when
    your setting a 32 bit variable to a 16 bit macro definition. I have seen programs silently die for example
    when a 32 bit coordinate is requested from a scrolling control with a 16 bit limit.

    #defines are pretty much just a text replacement, and macros can break your code in funny ways.

    Also the preprocessor step occurs before your compilation process, so the compiler doesn't have
    an opportunity to spit errors at you.
    Last edited by ahoodin; May 29th, 2009 at 02:35 PM.
    ahoodin
    To keep the plot moving, that's why.

  10. #10
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Are #defines evil?

    To quick work around is to use #undef to undefine the macro in your cpp file. As long as the const is being defined as private in the cpp file, it shouldn't conflict with the rest of the code.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

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