Skip to content

Instantly share code, notes, and snippets.

@JakobOvrum
Created October 22, 2015 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakobOvrum/74ead9a1d87dd627f5cc to your computer and use it in GitHub Desktop.
Save JakobOvrum/74ead9a1d87dd627f5cc to your computer and use it in GitHub Desktop.
/// 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