avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Can enumerator values be used in a #if preprocessordi


From: David Brown
Subject: Re: [avr-gcc-list] Can enumerator values be used in a #if preprocessordirective?
Date: Tue, 30 Aug 2011 10:03:08 +0200
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20110812 Thunderbird/6.0

On 29/08/2011 22:03, Dave Hansen wrote:


 > From: address@hidden
[...]
 > So that explains the difference. Seems like there could be a better
 > error message for this case, 'don't use enum here', alas "The
 > preprocessor does not know anything about types in the language"...

Here's a cute/ugly little macro that might help you find the errors
you're looking for:

#define ctassert(n,e) extern unsigned char n[(e)?0:-1]

Here's an example of how you might use it:

enum{a,b,c,d,last};
#define MAX 2
ctassert(big_enough,(last <= MAX));

Note that the macro does not need to be invoked from within a function.
If the test fails, the error message is almost pretty good, depending on
what you choose for the array name (sztest.c:6: error: size of array
`big_enough' is negative).

Refinements of the concept left as an exercise for the reader. Regards,


Does that mean I am not allowed to post my refinements? Hint - you want to use typedefs to avoid making extra symbols, you want to make sure you can use it as many times as you want in the same file, and you want to be able to use it inside functions as well as outside. And if you are like me and like extra warnings to keep your source code neat, it has to work with -Wnested-externs and -Wredundant-decls as well.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]