Testing for template argument being result of takeExactly

monarch_dodra monarchdodra at gmail.com
Sun Sep 23 14:32:24 PDT 2012


On Sunday, 23 September 2012 at 20:56:42 UTC, Ali Çehreli wrote:
> On 09/23/2012 12:02 PM, monarch_dodra wrote:
> > On Saturday, 22 September 2012 at 23:53:28 UTC, Jonathan M
> Davis wrote:
> >> I'm trying to test...
> >> [SNIP]
> >> - Jonathan M Davis
> >
> > I *kind of* see what you are doing with U, V, W, but what's
> wrong with
> > just doing:
> >
> > ----
> > import std.range;
> >
> > template Hello(R)
> > if(is(typeof(R == typeof(takeExactly(R, 1)))))
> > {
> > alias R Hello;
> > }
> >
> > struct S;
> >
> > void main( ) {
> > Hello!(int[]) a; //OK
> > Hello!S b; //FAIL
> > }
> > ----
> > ?
> >
> > It seems to work for me..., but I'm not 100% sure there isn't
> something
> > illegal/unsafe in there.
>
> The goal is to "test whether a template argument is the type 
> returned by takeExactly."
>
> Your solution (I think) is looking at a container and 
> determining whether the type of that container can be compared 
> to the return type of takeExactly(). (Note that typeof(X == Y) 
> is used for checking whether that expression itself is valid.)

Good point. My bad then.

> Also, I think your code should have passed a range like R.init 
> to takeExactly, not R, which is a type:
>
>   takeExactly(R.init, 1)
>
> I don't know why your code compiles.

That did seem fishy to me too actually, but it does compile.

> Ali

In that case, what about:

--------
import std.range;

template Hello(R)
     if(is(typeof(takeExactly(R.init, 1))) && is(R ==
typeof(takeExactly(R.init, 1))))
{
     alias R Hello;
}

struct S{};

void main( ) {
      Hello!(int[]) a; //OK
      Hello!S b;       //FAIL (again)
}
-------


More information about the Digitalmars-d-learn mailing list