__traits(compiles) and template instantiation

jkpl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 7 17:02:22 PDT 2016


On Thursday, 7 April 2016 at 21:36:37 UTC, Alex Parrill wrote:
> On Thursday, 7 April 2016 at 20:31:12 UTC, jmh530 wrote:
>> 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.
>> [...]
> Neither the third nor sixth lines should be true.
>
>     alias wrongfoo = foo!int; /* Error: template instance 
> foo!int does not match template declaration foo(T, U)(T x, U y) 
> if (isNumeric!T && isNumeric!U) */
>     alias rightfoo = foo!(int, int); /* ok */
>
> File a DMD bug.
>
> (Also, you can use static assert here to check the assertions 
> at build-time instead of run-time)

is(typeof()) gives the expected results:

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) { }

unittest
{
	static assert(is(typeof(foo!(int, int))));	//I get this
	static assert(!is(typeof(foo!(bool, bool))));	//I get this
	static assert(!is(typeof(foo!(int))));		//I think I get this
	static assert(is(typeof(bar!(int, int[]))));	//I get this
	static assert(!is(typeof(bar!(int, int))));	//I get this
	static assert(!is(typeof(bar!(int))));		//I don't get this
}

(note well I have changed the assertion 3 and 6).

There must be a subtle difference between __traits(compile,...) 
and is(typeof()).
Does "compiles" mean that you've get something but that this 
thing is not always of a valid type ?


More information about the Digitalmars-d-learn mailing list