Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Last active November 18, 2020 06:31
Show Gist options
  • Save PetarKirov/e12c7af4fefb9309eb492544d64dd960 to your computer and use it in GitHub Desktop.
Save PetarKirov/e12c7af4fefb9309eb492544d64dd960 to your computer and use it in GitHub Desktop.
D: Check if invariants are enabled using CTFE
bool invariantsEnabled() @trusted nothrow pure
{
static struct HasInvariant
{
@safe pure:
invariant { throw new Exception("From invariant"); }
void triggerInvaraint() { }
}
try
HasInvariant().triggerInvaraint();
catch (Exception)
return true;
return false;
}
enum hasInvariants = invariantsEnabled();
void main()
{
import std.stdio : writeln;
pragma (msg, hasInvariants);
writeln(invariantsEnabled());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment