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:
http://www.eetimes.com/discussion/programming-pointers/4025549/Catching-errors-early-with-compile-time-assertions

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]; \
        } STATIC_ASSERT_NAME_(__LINE__)




_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.org
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Reply via email to