Template specialization

Derek Parnell derek at nomail.afraid.org
Tue Apr 10 17:56:21 PDT 2007


On Tue, 10 Apr 2007 19:51:36 +0900, Bill Baxter wrote:

> Derek Parnell wrote:
>> What is the point of specialization if the compiler can't tell the
>> difference between data types? I expected that the point of using templates
>> is to make generic programming simpler.
> 
> The way I've understood it is that specialization just doesn't work with 
> IFTI...
> ... Use 'static if' checks 
> inside the template if you want to have both IFTI and specialization. 

Ok, I've tried all the reasonable ways to code this but the magic syntax to
do it evades my attempts. This whole area of compile-time capabilities is
very poorly documented.

Anyway, below is the code I think should work, because it says exactly what
I want the compiler to know. Namely, if the type (T) used in the code is
and "int" then do the 'int'-stuff, etc ...

 template fill_data(T)
 {
    void fill_data(char[] raw_data, out T t)
    {
        static if (T == int) // LINE 42
        {
            t = makeInt( raw_data );
        }
        else
        static if (T == float)
        {
            t = toFloat( raw_data );
        }
        else
        {
            fscopy(t, raw_data);
        }
    }
 } 

but this gives me messages that do not make sense at all to me ...

 test2.d(42): found ')' when expecting '.' following 'int'
 test2.d(43): found '{' when expecting identifier following 'int.'
 test2.d(44): found 't' when expecting ')'
 test2.d(44): found '=' instead of statement
 test2.d(46): Declaration expected, not 'else'
 test2.d(51): Declaration expected, not 'else'
 test2.d(54): unrecognized declaration

Can someone please show me the bleeding obvious correct syntax to use.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
11/04/2007 10:48:18 AM


More information about the Digitalmars-d-learn mailing list