Templates - Numeric Types only

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Aug 6 18:53:52 PDT 2009


Jarrett Billingsley wrote:
> On Thu, Aug 6, 2009 at 9:19 PM, Andrei
> Alexandrescu<SeeWebsiteForEmail at erdani.org> wrote:
>> Kevin wrote:
>>> Hi,
>>> simple Question:
>>> Is it possible to allow just numeric types in templates?
>>> For example to create a Vector Class like this:
>>>
>>> class Vector(T:"numeric" = float, int size = 4)
>>>
>>> Thanks,
>>> Kevin
>> import std.traits;
>>
>> class Vector(T, int size = 4) if (isNumeric!T)
>> {
>>    ...
>> }
> 
> Unfortunately,
> 
> struct Vec(T) if(isNumeric!T) {}
> struct Vec(T) {}
> Vec!(int) x; // error
> 
> foo.d(14): Error: template instance Vec!(int) matches more than one
> template declaration, Vec(T) if (isNumeric!(T)) and Vec(T)
> 
> *sigh*
> 
> Wouldn't it be nice.

struct Vec(T) if(isNumeric!T) {}
struct Vec(T) if(!isNumeric!T) {}

is actually superior because it's more modular; the semantics of the 
latter does not depend on the presence of the former.

Andrei



More information about the Digitalmars-d mailing list