Last active
November 18, 2020 06:31
-
-
Save PetarKirov/e12c7af4fefb9309eb492544d64dd960 to your computer and use it in GitHub Desktop.
D: Check if invariants are enabled using CTFE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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