std.path review: update

bearophile bearophileHUGS at lycos.com
Mon Jul 18 03:28:50 PDT 2011


Jonathan M Davis:

> And it _should_ be a template. All of the stuff like that are templates. And 
> I'm not even sure that it _can_ be a function. And even if it can, what would 
> we gain by making it a function anyway? It's operating on types. It's of no 
> use at runtime. It's a perfect candidate for an eponymous template. 
> std.traits, std.range, etc. do this sort of thing in pretty much exactly the 
> same way. There may be a cleaner way to write it then it currently is, but 
> using an eponymous template like that is the correct thing to do.

This seems to work:


import std.traits: isSomeChar, Unqual, isSomeString;

bool compatibleStrings(Strings...)() if (Strings.length) {
    static if (isSomeString!(Strings[0])) {
        alias Unqual!(typeof(Strings[0].init[0])) TC;
        foreach (s; Strings[1 .. $])
            static if (isSomeString!s && !is(TC == Unqual!(typeof(s.init[0]))))
                return false;
        return true;
    } else
        return false;
}

version (unittest) {
    static assert (compatibleStrings!(char[], const(char)[], string)());
    static assert (compatibleStrings!(wchar[], const(wchar)[], wstring)());
    static assert (compatibleStrings!(dchar[], const(dchar)[], dstring)());
    static assert (!compatibleStrings!(int[], const(int)[], immutable(int)[])());
    static assert (!compatibleStrings!(char[], wchar[])());
    static assert (!compatibleStrings!(char[], dstring)());
}

void main() {}

I have written tons of such things in dlibs1, and generally I have seen that recursive templates are slower and need more RAM than similar functions.

Bye,
bearophile


More information about the Digitalmars-d mailing list