Testing implicit conversion to template instance with is() expression

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 15 09:53:33 PDT 2015


On Sunday, 15 March 2015 at 16:44:14 UTC, Ali Çehreli wrote:
> On 03/15/2015 08:47 AM, "Marc =?UTF-8?B?U2Now7x0eiI=?= 
> <schuetzm at gmx.net>" wrote:
>
> > Should this work?
> >
> >      struct V(string s) {
> >      }
> >
> >      struct S(int U) {
> >          V!"xyz" x;
> >          alias x this;
> >      }
> >
> >      void main() {
> >          S!10 a;
> >          static assert(is(a : V!Args, Args...));
> >      }
> >
> > With DMD Git master, the static assert() fails. Should it? Am
> I doing
> > something wrong? How can I test whether something is
> implicitly
> > convertible to any instance of a particular template?
>
> There is no way other than checking for compile-time duck 
> typing (see the implementations of isInputRange and others).
>
> One reason is that the compiler does not have the concept of 
> "an instance of a template". Templates are for code generation 
> and only the end-result (i.e. S!10) lives as a concept when 
> compiling.

The code contained a small mistake, I forgot a `typeof()`:

     // static assert(is(a : V!Args, Args...));
     // should be:
     static assert(is(typeof(a) : V!Args, Args...));

This still fails, but it works when I change it to:

     static assert(is(typeof(a) : S!Args, Args...));

This means I can indeed test whether something _is_ an instance 
of a template. It just doesn't take the `alias this` into 
account. So I guess that's a bug?


More information about the Digitalmars-d-learn mailing list