__traits(compiles) and template instantiation
jmh530 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Apr 7 13:31:12 PDT 2016
I've been playing around with __traits and I find myself confused
on one aspect. In the code below, I was testing whether some
templates would compile given types. For the most part it works
as I would expect.
I think I get why the third one works with foo!(int). My guess is
that it assumed that U is the same as T and both are int.
However, that wouldn't make sense with the last one where I use
bar!(int). In that one it's basically ignoring the second
template constraint. So I don't understand what's going on for
that last line to compile. To confirm I wasn't crazy, I get an
error with
alias bar_ = bar!(int);
import std.traits : isNumeric;
import std.range : isInputRange;
void foo(T, U)(T x, U y) if (isNumeric!T && isNumeric!U) { }
void bar(T, U)(T x, U y) if (isNumeric!T && isInputRange!U) { }
void main()
{
assert(__traits(compiles, foo!(int, int))); //I get this
assert(!__traits(compiles, foo!(bool, bool))); //I get this
assert(__traits(compiles, foo!(int))); //I think I get this
assert(__traits(compiles, bar!(int, int[]))); //I get this
assert(!__traits(compiles, bar!(int, int))); //I get this
assert(__traits(compiles, bar!(int))); //I don't get this
}
More information about the Digitalmars-d-learn
mailing list