Assert and undefined behavior

Eduard Staniloiu edi33416 at gmail.com
Wed Oct 11 20:07:36 UTC 2017


On Wednesday, 11 October 2017 at 09:39:04 UTC, user1234 wrote:
> On Wednesday, 11 October 2017 at 09:27:49 UTC, John Burton 
> wrote:
>> [...]
>> I therefore feel like I ought to not use assert and should 
>> instead validate my assumptions with an if statement and a 
>> throw or exit or something.
>
> Yes, that's the way of doing. assert() are just used to test 
> the program. the -release option in DMD disable all the 
> assert() (excepted assert(0) which is a bit special), so that 
> in a release version, only Throwable objects can be used after 
> a failure detected.

A small addition to the answers already provided.

As user1234 has already said, asserts are removed in the -release 
build, so, if you have to validate some assumption (ex. the file 
opened) you should use enforce[0].

Cheers,
Eduard

[0] - https://dlang.org/library/std/exception/enforce.html


More information about the Digitalmars-d-learn mailing list