Static asserts within unittest block

Jonathan M Davis jmdavisProg at gmx.com
Tue Mar 29 10:53:34 PDT 2011


On 2011-03-29 10:08, Piotr Szturmaj wrote:
> I see this is common practice in Phobos. I though static asserts should
> be checked at each compilation, not only when compiling with unittest.
> Or is it supposed to shorten compile time for already tested modules? :)

If a static assert is in a unit test block it's to verify that something 
works. You don't necessarily want it in normal code. For instance, what if the 
static assert is verifying something about a templated type or function? 
Having that static assert in the normal code would mean that that particular 
instantiation of the function would always be in the code, whereas putting the 
static assert in the unit test would only result in that instantiation during 
unit tests (unless you actually instantiated it in your code).

Whether static assert is used in normal code or a unit test depends entirely 
on what's being tested. A common place to use it in normal code would be with 
version blocks. e.g.

version(Posix)
{
}
else version(Windows)
{
}
else
	static assert(0, "Unknown OS version.");

static assert is a tool like any other and the best way to use it depends on 
what you're trying to do and what the situation is.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list