Testing for template argument being result of takeExactly

Timon Gehr timon.gehr at gmx.ch
Sun Sep 23 05:47:27 PDT 2012


On 09/23/2012 01:54 AM, Jonathan M Davis wrote:
> I'm trying to test whether a template argument is the type returned by
> takeExactly, and I haven't been able to sort out the template voodoo required
> yet.  It would be a lot easier if I had a variable to work with, but I just
> have the type, and the fancy is expression required to pull it off is fancy
> enough that I haven't been able to sort it out yet. At present, I have this:
>
> import std.range;
> import std.stdio;
>
> template Hello(R)
>      if(is(R r == U, V, V w, U = typeof(takeExactly(w, 1))))
> {
>      alias R Hello;
> }
>
> void main()
> {
>      auto str = "hello";
>      auto t = takeExactly(str, 3);
>      writeln(t);
>      Hello!(typeof(t)) h = t;
>      writeln(h);
> }
>
>
> I need Hello to instatiate if R is the type returned by takeExactly and fail
> to instantiate otherwise. At present, the code gives these compilation errors:
>
> q.d(15): Error: template instance Hello!(Result) Hello!(Result) does not match
> template declaration Hello(R) if (is(R r == U,V,V w,U =
> typeof(takeExactly(w,1))))
> q.d(15): Error: Hello!(Result) is used as a type
> q.d(16): Error: template std.stdio.writeln does not match any function
> template declaration
> q.d(16): Error: template std.stdio.writeln(T...) cannot deduce template
> function from argument types !()(_error_)
>
> So, clearly I don't have the is expression right, and this is seriously
> pushing the edge of my knowledge of is expressions. So, any help would be
> appreciated. Thanks.
>
> - Jonathan M Davis
>

import std.range, std.traits;
import std.stdio;
template Hello(R) if(is(typeof(R._input.takeExactly(2)) == R)){
     alias R Hello;
}

void main(){
     auto str = "hello";
     auto t = takeExactly(str, 3);
     writeln(t);
     Hello!(typeof(t)) h = t;
     writeln(h);
}


More information about the Digitalmars-d-learn mailing list