Testing for template argument being result of takeExactly

Jonathan M Davis jmdavisProg at gmx.com
Sat Sep 22 16:54:14 PDT 2012


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


More information about the Digitalmars-d-learn mailing list