What is the syntax to test for specific types in templates

Derek Parnell derek at psych.ward
Wed Apr 12 01:08:55 PDT 2006


On Wed, 12 Apr 2006 06:01:52 +0000 (UTC), Oskar Linde wrote:

> In article <138zdzputeyti$.16ay0g5ad0xj6$.dlg at 40tude.net>, Derek Parnell says...
>>
>>I want to ensure that the template can only be generated for certain data
>>types:...
>>I think it should be something like ...
>>
>>template foo(T)
>>{
>>   T foo(T x)
>>   {
>>      static if ( is(ulong T) || ...
> 
> static if (is(T == ulong)) 
> or maybe better for your application:
> is (T : int)
> for all T implicitly convertible to int.
> 
> /Oskar

Thanks, that works fine.

In the docs is says "is ( Type == TypeSpecialization )" is one of the
forms, but I had a mental block as I didn't recognise that 'Type' meant the
template's 'type' parameter. A simple example of this form would be helpful
...

template foo(T)
{
T foo(T bar)
{
    static if ( is(T == real)   ||
                is(T == double) ||
                is(T == float) )
    {
         // Code for floating point types
    }
    else static if ( is(T == ulong) ||
                     is(T == long)  ||
                     is(T == uint)  ||
                     is(T == int)   ||
                     is(T == ushort)||
                     is(T == short) ||
                     is(T == ubyte) ||
                     is(T == byte) )
    {
        // Code for integer types
    }
    else
    {
        pragma(msg, "ERROR! foo!() Can only use an integer or floating
point type");
        static assert(0);
    }
}
}

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
12/04/2006 6:04:38 PM



More information about the Digitalmars-d mailing list