Created
October 22, 2015 10:13
-
-
Save JakobOvrum/74ead9a1d87dd627f5cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/// Returns true iff $(D func(args)) can be evaluated at compile-time | |
/// and doesn't error when evaluated. | |
template runsWithoutError(alias func, args...) | |
if(__traits(compiles, func(args))) | |
{ | |
// Wrapper anon func so it works with `func`s that have void return type | |
enum exec() = () { func(args); return true; /* never used */ }(); | |
//enum runsWithoutError = exec!(); | |
enum runsWithoutError = __traits(compiles, exec!()); | |
} | |
void test(string s) | |
{ | |
assert(s == "foo"); | |
} | |
static assert(runsWithoutError!(test, "foo")); | |
static assert(!runsWithoutError!(test, "bar")); | |
import std.conv : to; | |
static assert(runsWithoutError!(to!int, "42")); | |
static assert(!runsWithoutError!(to!int, "foo")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment