CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 1999
    Posts
    137

    enum arithmetic by the preprocessor

    We have a bunch of enum values in our code. We need a string in the string table for each one.

    MyFile.h
    Code:
    enum FOO
    {
        FOO_BASE = 0,
        FOO_1,
        FOO_2,
    
        FOO_MAX
    };
    resource.h
    Code:
    #define IDS_BAR_BASE  4096
    #define IDS_BAR_1     4097
    #define IDS_BAR_2     4098
    #define IDS_BAR_MAX   4099
    I am trying to create a sanity check in case more FOO's or BAR's are added.

    MyFile.cpp
    Code:
    #if ((FOO_MAX - FOO_BASE) != (BAR_MAX - IDS_BAR_BASE))
        #error Each FOO in Myfile.h must have its corresponding BAR string in resource.h. 
    #endif
    This does not work. The #if evaluates TRUE, the #error is compiled, and I get an error.

    A little experimentation shows that the preprocessor is evaluating FOO_MAX as 0.

    I have tried a few variations on this, such as ((int)FOO_MAX). Nothing works.

    What is wrong? How do I fix it?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: enum arithmetic by the preprocessor

    A little experimentation shows that the preprocessor is evaluating FOO_MAX as 0.
    Of course it does since it does not evaluate it at all. The preprocessor evaluates macros and preprocesor directives only. The compiler is the one that evaluates the enumerations. So, your solution is doomed to fail.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Sep 1999
    Posts
    137

    Re: enum arithmetic by the preprocessor

    You are right. It took me a while to see that.

    The development environment does evaluate it if you hold the mouse over it. I was thinking the preprocessor did the same. My bad.

    I will make it an ASSERT.

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: enum arithmetic by the preprocessor

    The development environment does evaluate it if you hold the mouse over it. I was thinking the preprocessor did the same. My bad.
    That's the IntelliSense working there. It builds a database with information about your types, variables, functions, etc. by analyzing the code. It is keep it in a file with the extension .ncb.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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