Rahul said:
Its this,
#define COMPILE_TIME_ASSERT(expr) do \
{ \
int temp[(expr) != 0 ? 1 : -1]; \
(void)(temp);\
} while(0)
So the following will casuse a compile time error.
COMPILE_TIME_ASSERT(2-2);
(It would help if you'd follow the newsgroup guidelines, and quote what
you're responding to.)
How are you using this macro? You haven't shown the code using this, or the
structure you're using it on. You should always post _complete_ information
here, or else we're left groping in the dark.
Why are you using this declaration?:
int temp[(expr) != 0 ? 1 : -1];
Your code appears to check for inequality, as if all you want is for the two
numbers to be different. I thought you wanted to be sure the _order_ of the
members was correct. How are you doing that? What good does it do to only
accept _unequal_ values? (Hint: show the code!)
Anyway...
I did the following test, and it correctly tells me whether two members are
in the expected order or not:
struct TS1
{
// ok, correct order (a before b)
int a;
char b;
};
struct TS2
{
// not ok, wrong order (a after b)
char b;
int a;
};
#define COMPILE_TIME_ASSERT2(expr) do \
{ \
int temp[(expr)]; \
(void)(temp);\
} while(0)
int main()
{
COMPILE_TIME_ASSERT2(offsetof(TS1,b)-offsetof(TS1,a)); // ok
COMPILE_TIME_ASSERT2(offsetof(TS2,b)-offsetof(TS2,a)); // not ok
// ...
}
That code accepts TS1 fine, but for TS2 it reports that I can't have an
array larger than ffffffff bytes. So there's apparently a signed/unsigned
issue here. If I change the array definition like this:
int temp[(long)(expr)]; \
then I get the expected "negative subscript" error for TS2.
So, I think I've shown that offsetof is (at least with my compiler) a
compile-time value, eh? Try my code on your compiler. If it still fails
(by not causing an error), then you've got a non-compliant compiler.
-Howard