Conditional Templates

Derek Parnell derek at nomail.afraid.org
Sun Jan 28 22:28:51 PST 2007


Help! The D documentation makes me seem such a fool. 

Here is what I want to do ...

I want to create a template for a single class member method that behaves
differently depending on the type of the one parameter supplied to the
method. In other words, if the method is called with a integer type of
parameter, D should instantiate one form of the member method, and if the
parameter is a floating point data type, D should instantiate another form
of the member method, and finally if the parameter is a specific class, D
should instantiate yet another form of the member method.

So in 'pseudo' code ...

class Bar
{
 template Foo(T)
 {
    static if (T is an integer type) // byte, ubyte, short, ushort, ...
    {
        void Foo(T x)
        {
             // do something with the integer 'x'
        }
    }

    static if (T is a floating point type) // float, double, real
    {
        void Foo(T x)
        {
             // do something with the floating point value.
        }
    }

    static if (T is the class 'Bar')
    {
        void Foo(Bar x)
        {
             // do something with this object.
        }
    }

    // All other forms are illegal.
 }
}

I've searched the docs and failed to solve how this can be done, if at all.
I'm sure there are some examples in the newsgroup postings but its like
looking for something that you don't know what it actually looks like.

I did note that the docs say you can't use templates to add non-static
class members, but I tried and it works fine; so I don't know if the docs
are wrong, I'm wrong or DMD is wrong.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
29/01/2007 5:17:20 PM


More information about the Digitalmars-d-learn mailing list