assert(false) in release == splinter in eye

Jonathan M Davis jmdavisProg at gmx.com
Sun Oct 10 23:18:43 PDT 2010


On Sunday 10 October 2010 08:52:28 Christopher Bergqvist wrote:
> Hi,
> 
> Time for some Sunday nitpicking. While reading TDPL, one thing that stuck
> out to me was the special behavior of assert(false). Consider the following
> program compiled with "-release".
> 
> void main()
> {
>     int a = 0;
>     assert(a);
> }
> 
> That program will run without errors. Changing the type of variable a from
> "int" to "enum" results in the program segfaulting thanks to the compiler
> being able to know the value of the expression "a" at compile time and
> inserting a HLT/"halt" instruction. Having the ability to change something
> subtle in a more complex expression or series of expressions without
> realizing you made a compile time assert(false) which crashes your program
> feels ugly.
> 
> I would prefer it if assert() didn't have this special type of behavior,
> and that a "halt" keyword or equivalent was introduced. What do you think?
> 
> / Chris

I really don't see the problem. Why would you feed an enum to assert as a test? 
assert is for testing stuff at runtime. If you want to test stuff at compile time, 
you use static assert. You're not going to accidentally shoot yourself in the 
foot with this unless you change a variable to an enum and forget to change your 
assert to a static assert. And since assertions are usually used on function 
arguments and member variables - _none_ of which would be compile-time constants 
- I really don't see the problem.

- Jonathan M Davis


More information about the Digitalmars-d mailing list