April 12, 2006
I want to ensure that the template can only be generated for certain data types: integers and floating-point numbers only. I've seen something like this done before but I cannot work it out fro the official documentation.

I think it should be something like ...

template foo(T)
{
   T foo(T x)
   {
      static if ( is(ulong T) ||
                  is(long T)  ||
                  is(uint T)  ||
                  is(int T)  ||
                  is(ushort T)  ||
                  is(short T)  ||
                  is(ubyte T)  ||
                  is(byte T) )
      {
         // ... code for integers ... //
      }
      else
      static if ( is(real T) ||
                  is(double T)  ||
                  is(float T) )
      {
         // ... code for floating points ... //
      }
      else
      {
         pragma(msg, "Only integers and floating points");
         static assert(0);
      }

   }
}

I know I can use template specialization but that is *way* too cumbersome as I'd need three copies of the floating point template and eight copies of the integer template. And seeing that templates were invented to avoid all this copying it seems odd that there is not easy way to do multiple specialization.

Even if we could do ...

  template foo(T : (ulong, long, uint, int, ushort, short, ubyte, byte) )
  {
    // code for integers
  }

  template foo(T : (real, double, float) )
  {
    // code for floating points
  }

would be useful but I don't think anything like this is allowed.

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
12/04/2006 2:07:10 PM
April 12, 2006
In article <138zdzputeyti$.16ay0g5ad0xj6$.dlg@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


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

> In article <138zdzputeyti$.16ay0g5ad0xj6$.dlg@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
Top | Discussion index | About this forum | D home