is(some template instantiation) is true, but the actual instantiation fails

Adrian Matoga via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 29 09:01:46 PST 2016


On Friday, 29 January 2016 at 16:36:01 UTC, Steven Schveighoffer 
wrote:
> On 1/29/16 10:28 AM, Adrian Matoga wrote:
>> Code:
>>
>> ----
>> struct HasFoo { void foo() {} }
>>
>> struct NoFoo {}
>>
>> struct CallsFoo(T) {
>>      T t;
>>      void bar() { t.foo(); }
>> }
>>
>> static assert(is(CallsFoo!HasFoo));
>> alias Bar = CallsFoo!HasFoo;
>>
>> static assert(is(CallsFoo!NoFoo)); // (1)
>> //alias Baz = CallsFoo!NoFoo;      // (2)
>> ----
>>
>> This compiles, although I expected that (1) should fail.
>> Now try uncommenting (2) and it can't be compiled.
>>
>> Why does `is(CallsFoo!NoFoo)` evaluate to true if 
>> `is(CallsFoo!NoFoo)`
>> can't be instantiated?
>> Am I missing something about `is(T)` or is it a bug?
>> How can I reliably test if CallsFoo can be instantiated?
>>
>
> is(T) is supposed to be false if T is not a valid type.
>
> I would agree with you that the static assert should fail.
>
> -Steve

Oh, there's more:
// this should fail:
static assert(is(CallsFoo!NoFoo));
// this should fail too:
static assert(is(typeof({ alias Baz = CallsFoo!NoFoo; return 
Baz.init; }())));
// and this:
static assert(__traits(compiles, { alias Baz = CallsFoo!NoFoo; 
return Baz.init; }()));
// but only this fails:
alias Baz = CallsFoo!NoFoo;

https://issues.dlang.org/show_bug.cgi?id=15623



More information about the Digitalmars-d-learn mailing list