[Top][All Lists]

[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 14:45:15 +0200
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20110812 Thunderbird/6.0

On 30/08/2011 14:03, Bob Paddock wrote:
Refinements of the concept left as an exercise for the reader. Regards,

Does that mean I am not allowed to post my refinements?

No.  Please post.  Extra points for using __FILE__ __FUNCITON__ and
__LINE__ to give meaningful error message.

The error message already contains information about the line number, just like for any other error message.

You can't get very meaningful errors here - I have not found any way to get a user-defined error message in conjunction with static asserts. There is, AFAIK, an effort to get standardised static assertions into both C and C++ with useful messages. But in a fine example of mind-blowing stupidity, C++0x uses
        static_assert(expression, message)
while C1x calls it _Static_assert.

If your compiler supports static_assert or _Static_assert (gcc 4.6 onwards) then you are best using it directly.

Digging out my old issue of Embedded Systems on Compile Time Assertions
we find this:

One thing to watch out for in some of the examples I've seen is in C99
a zero length variable array could be created.

My macros (message is for compatibility with future C/C++ standards, and self-documentation of the code - it is not used by the macro):

#define STATIC_ASSERT_NAME_(line)       STATIC_ASSERT_NAME2_(line)
#define STATIC_ASSERT_NAME2_(line)      assertion_failed_at_line_##line

#define static_assert(claim, message) \
        typedef struct { \
                char STATIC_ASSERT_NAME_(__LINE__) [(claim) ? 1 : -1]; \

reply via email to

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