Template specialization

Derek Parnell derek at nomail.afraid.org
Tue Apr 10 19:19:30 PDT 2007


On Wed, 11 Apr 2007 10:24:20 +0900, Bill Baxter wrote:

> Derek Parnell wrote:
>> 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.
>> 
> 
> You need some is's there like
>      static if (is(T==int))

Thank you. 

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.

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);
        }
    }
 }

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
11/04/2007 12:08:05 PM


More information about the Digitalmars-d-learn mailing list