class specialization for integral types

Jonathan M Davis jmdavisProg at gmx.com
Sat Sep 24 14:33:14 PDT 2011


On Saturday, September 24, 2011 14:16:12 Charles Hixson wrote:
> How would a specialize a parameterized class to only allow integral
> parameters (i.e., anything that would respond "true" to
> static if (is (T : long) )
> 
> (I'd have said "static if (is (T : cent) )" or "static if (is (T :
> ucent) )", but those are still marked "reserved for future use".)
> 
> I do want to allow byte, ubyte, etc. to be legal values.

Parameterized? As in templated? Just use a template constraint.

class C(T)
    if(is(T : long))
{
}

T will then be allowed to be anything which is implicitly convertible to long. 
I'd suggest using std.traits.isIntegral instead though.

class C(T)
    if(isIntegral!T)
{
}

It specifically tests for whether the type is a byte, ubyte, short, ushort, 
int, uint, long, or ulong, so it won't included stray structs or classes which 
would be implicitly convertible to long, and if/when eont and ucent come 
along, they'd be added to isIntegral and be automatically supported.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list