|
-
November 27th, 2007, 09:22 PM
#1
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?
-
November 28th, 2007, 02:14 AM
#2
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.
-
November 28th, 2007, 12:07 PM
#3
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.
-
November 28th, 2007, 05:20 PM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|