One question about templates

bearophile bearophileHUGS at lycos.com
Thu Aug 5 03:28:49 PDT 2010


Philippe Sigaud:

> I used to be confronted to this pb too. Here is what I did:
> ...

You are gentle and helpful, and your post has given me useful ideas, but your post it too much unreadable for me.

If you take a look here you can see 16 attaches, and it seems part of your message are missing, where is the code for TemplateName? And what does it happen if two templates coming from two modules share the same name?
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20943

Through your post I have found this, that can't be used to solve my problem, but seems interesting and worth putting into Phobos2 if not already present:

private string[3] _between(char b, char e, string s)() {
    int foundb, ib;
    string notFound = "";

    foreach (i, c; s) {
        if (c == b) {
            if (foundb == 0) {
                foundb = 1;
                ib = i+1;
                continue;
            } else {
                foundb++;
            }
        }

        if (c == e) {
            if (foundb == 1)
                return [s[0 .. ib-1], s[ib .. i], s[i+1 .. $]];
            else
                foundb--;
        }
    }

    return [s, notFound, notFound];
}

template TemplateParameters(T) {
    mixin("alias TypeTuple!(" ~ _between!('(', ')' , T.stringof)[1] ~ ") TemplateParameters;");
}

But in the end I have not understood how you suggest me to solve my problem, sorry :-)

Writing code like this is not a good idea because I think it instantiates extraneous templates:
assert(is(Foo!(TemplateParameters!(S1)) == S1));

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list