-
Notifications
You must be signed in to change notification settings - Fork 2.1k
core/assert: provide static_assert for c99 #9279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the semicolon. The user will write a semicolon when using it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put spaces around the division operator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK, please squash
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: