simple template constraint - compile error.

WhatMeWorry via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 7 16:08:01 PDT 2015


Should be real simple. But adding a small template constraint 
causes compile failure.  I've got the following working code:

int arrayByteSize(T)(T[] someArray)
{
     ubyte[] arr = cast(ubyte[]) someArray;
     return arr.length;        // length is implicitly converted 
to bytes.
}

// running the following code:

GLfloat[] dynamicVerts = [0.0, 1.0,-1.0, -1.0, 1.0, -1.0 ];

int lengthInElements = dynamicVerts.length;
myLog!lengthInElements;
int sizeInBytes = dynamicVerts.arrayByteSize;
myLog!sizeInBytes;

// returns the correct results:

lengthInElements = 6
sizeInBytes = 24


However, when I try to add a simple constraint to the function 
like so:

int arrayByteSize(T)(T[] someArray) if (isDynamicArray(T))
{
     ubyte[] arr = cast(ubyte[]) someArray;
     return arr.length;        // length is implicitly converted 
to bytes.
}


I get Error: cannot pass type float as a function argument

After I comment out //int sizeInBytes = 
arrayByteSize(dynamicVerts);

everything compiles. I've been fighting this for hours.  Am I 
overlooking the obvious?





More information about the Digitalmars-d-learn mailing list