Templates lots of newbie qs.

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Mar 8 07:54:08 PST 2007


"Chris Warwick" <sp at m.me.not> wrote in message 
news:esp8re$n7g$1 at digitalmars.com...
> Ok, never used templates before but i think i have the basic idea. Just 
> have a few questions and would welcome any corrections / sugestions on 
> way's to improve this, my first template.. (surprisingly had very little 
> trouble writing this and it compiling, not sure about working tho')

Okay, here is your code commented with answers (you were mostly right):

class vector(T)
{
    T[] fitems;
    // fitems, being a dynamic array reference, isn't initialized to 
anything.  It's a
    // zero-length array that points to nothing.  If you want it to be a 
certain length,
    // you need to make a constructor like the following:
    this(size_t length)
    {
        // Now here, yes, fitems is filled with the default value for T.
        fitems.length = length;
    }

    this(T[] source)
    {
        fitems = source.dup;
    }

    // Yes, this is entirely right.
    this(vector!(T) source)
    {
        fitems = source.fitems.dup;
    }

    void append(T item)
    {
        fitems ~= item;
    }

    bool remove(T item)    // returns true if item removed
    {
        int i = indexOf(item);
        if (i >= 0)
        {
            fitems[i..fitems.length-1] = fitems[i+1..fitems.length];
            fitems.length = fitems.length-1;
            return true;
        }
        else
        {
            return false;
        }
    }

    int indexOf(T item)    // returns -1 if not found
    {
        for (int i = 0; i < fitems.length; i++)
        {
            if (fitems[i] == item) { return i; }
        }
        return -1;
    }

    int length()
    {
        return fitems.length;
    }

    // Yes, these are also correct.
    // Array bounds checking is only disabled in the release build, and
    // can't be turned on and off per-module unless you compile this module
    // in release mode and the rest in debug (which is a pain).

    T opIndex(int i)
    {
        return fitems[i];
    }

    void opIndexAssign(T item, int index)
    {
        fitems[index] = item;
    }
}

>
> And are class templates value types or referances types?

They're classes, therefore they're reference types.  Templating just changes 
the types inside the class, not the class itself.

> How well optimized is stuff like this? Will the opIndex/Assign optimize 
> down to similar speed as using a basic array?

If you were to make those short methods (like opIndex and opIndexAssign) 
"final", and then used the -O -inline flags when compiling, I would imagine 
they'd be inlined.  Class method calls can't be inlined unless the methods 
are final, since the compiler can then turn a method call of a final method 
into a static function call.  Of course you then can't override the final 
method if you derive from the class.

> For basic array stuff like this would it be better to use the "Functions 
> as Array Properties" feature? Can that be templatized? Can you writed a 
> generic function that takes an array as the first param.. Somthing like 
> this...
>
> template ArrayProps(T)
> {
>     int indexOf(T[] arr, T item)
>    {
>        for (int i = 0; i < arr.length; i++)
>        {
>            if (arr[i] == item) { return i; }
>        }
>        return -1;
>    }
> }
>
> ArrayPros(int);
> int[] foo = [0,1,2,3,4];
> int ibetthisdontwork = foo.indexOf(3);

Yeah, for simple stuff like this array properties are probably better. 
You're also pretty close with your code there;  you can just write it as a 
templated function:

int indexOf(T)(T[] arr, T item)
{
    for(int i = 0; i < arr.length; i++)
    {
        if (arr[i] == item) { return i; }
    }
    return -1;
}

int[] foo = [0,1,2,3,4];
int thisdoeswork = foo.indexOf(3);

It's also using IFTI there to determine T implicitly.

You might check out the Cashew library 
(http://www.dsource.org/projects/cashew).  It has a bunch of these kinds of 
array functions in cashew.util.array. 




More information about the Digitalmars-d-learn mailing list