What is the difference between a static assert and a unit test?

frame frame86 at live.com
Thu Apr 21 22:54:08 UTC 2022


On Thursday, 21 April 2022 at 22:26:57 UTC, Alain De Vos wrote:
> I don't know when to use a static assert and when to use a unit 
> test ?

There is `assert()`, `static assert()` and `unittest`.

`static assert()` is used while compiling, to find errors or 
circumstances that can lead to errors in runtime or just show 
type incompatibilities.

`assert()` is used at runtime to allow some checks before 
processing data.  It is evaluated at runtime and removed from 
code in release mode. Except `assert(0)` which will always emit 
an `AssertError` and will be never removed.

`unittest` is a just block that will be only compiled/executed 
when the `-unittest` switch will be applied to compiler command 
line. That's it.



More information about the Digitalmars-d-learn mailing list