Testing for template argument being result of takeExactly
Philippe Sigaud
philippe.sigaud at gmail.com
Sun Sep 23 04:55:04 PDT 2012
On Sun, Sep 23, 2012 at 1:54 AM, Jonathan M Davis <jmdavisProg at gmx.com> 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.
Seeing that takeExactly returns itself when its input is a
takeExactly, I used this:
import std.range;
import std.stdio;
template Hello(R)
{
static if (is(typeof(R._input)) // Is using R._input OK?
&& is(typeof(takeExactly(typeof(R._input),0)) == R )) //
Does applying takeExactly on R gives R back?
alias typeof(R._input) Hello; // Extract the original range type
else
alias void Hello;
}
void main()
{
auto str = "hello";
auto t = takeExactly(str, 3);
writeln(t);
writeln(Hello!(typeof(t)).stringof); // "string"
}
Transforming it into a predicate template is easy, I just wanted to
show that R._input is accessible.
Caution: takeExactly returns a slice when the range is slice-able. You
should test for hasSlicing on R first.
As for Voldemort (Nameless One, Dark Lord, whatever) types, I find
them more annoying than useful, personally.
Philippe
More information about the Digitalmars-d-learn
mailing list