core/assert: provide static_assert for c99#9279
Conversation
jnohlgard
left a comment
There was a problem hiding this comment.
Clever solution, I like it! Minor code style comments
core/include/assert.h
Outdated
| */ | ||
| #define static_assert(...) struct static_assert_dummy | ||
| #define static_assert(cond, ...) \ | ||
| enum { static_assert_failed_on_div_by_0 = 1/(!!(cond)) }; |
There was a problem hiding this comment.
Remove the semicolon. The user will write a semicolon when using it
There was a problem hiding this comment.
Put spaces around the division operator
|
Thanks, but I can't take credit. I looked for a way of doing it via a web search and this was suggested in several places as a way of implementing static_assert. The more common way of doing it apparently is by a typedef for an array with a negative number of members if the condition fails, but because RIOT is set up to be very strict about code quality it gives a warning about an unused typedef. The enum doesn't give any warnings when it is unused. |
429a497 to
2bd1d7f
Compare
|
@gebart Squashed and did a double-check to make sure it still works correctly with the semicolon removed. Should be good to go. |
|
Everything is good here. Let's go! |
This provides a working static_assert for c versions < c11. Should work globally and locally.
Required by #9190 for detecting collisions in gpio_t definitions.
Example return: