Template specialization

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Apr 10 20:07:04 PDT 2007


"Derek Parnell" <derek at nomail.afraid.org> wrote in message 
news:1x1oqnct1uxwh$.1hnmedvjme3hd$.dlg at 40tude.net...
>
> I ended up with ...
>
> template fill_data(T)
> {
>    void fill_data(char[] raw_data, T* t)
>    {
>        static if (is(T == int) )
>        {
>            *t = makeInt( raw_data );
>        }
>        else
>        static if (is(T == float))
>        {
>             *t = toFloat( raw_data );
>        }
>        else
>        {
>              fscopy(t, raw_data);
>        }
>    }
> }
>
> Besides your corrected syntax, I had to change the function signature to
> use pointers rather than use the 'out' modifier as D gets upset when using
> 'out' with fixed-size arrays. I don't know why it should because doing so
> makes it yet another exception for writing generic code.

Don't you hate them?  They seem like they'd be so useful, too, if they could 
be returned and used as out parameters.

> Also, I found out that the 'static if' only compiles if it is inside the
> function declaration - another dumb restriction, IMNHO. Meaning that I
> couldn't do the obvious thing to avoid using pointers ...
>
> template fill_data(T)
> {
>    static if (is(T == int) )
>    {
>        void fill_data(char[] raw_data, out T t)
>        {
>            t = makeInt( raw_data );
>        }
>    else
>    static if (is(T == float))
>    {
>        void fill_data(char[] raw_data, out T t)
>        {
>             t = toFloat( raw_data );
>        }
>    }
>    else
>    {
>        void fill_data(char[] raw_data, T t)
>        {
>              fscopy(t, raw_data);
>        }
>    }
> }

You're missing a closing brace on that first static if clause.  But even 
still, this is no longer a function template and can then no longer be 
called using IFTI. 




More information about the Digitalmars-d-learn mailing list