I'd like to use static_assert
in my C 11 project to do some compile time check.
As my understanding, static_assert
won't be executed at runtime, right?
If so, when I compile my project by executing the command about compilation, such as gcc ...
, the compiler will build the static_assert
into the binary file or the static_assert
will be totally ignored, just like a comment?
CodePudding user response:
Is static_assert compiled into the binary file
No.
As my understanding, static_assert won't be executed at runtime, right?
Right.
the compiler will build the static_assert into the binary file
No.
the static_assert will be totally ignored, just like a comment?
No, it's not a comment - the expression is checked, and if the expression is false, then a message is shown. When the expression is not a constant (can't be computed at compile time) then also a message is shown.
CodePudding user response:
Unlike the old assert()
, which is a runtime test, the static_assert
is tested by the compiler and results in a compile time error if not satisfied. Or if it cannot be tested statically...
Then, at runtime, it is already known to be true and does not need to be tested again.