On Saturday, 5 January 2013 at 21:50:19 UTC, ref2401 wrote:
I have a template function that must work with an array of float values.
something like this:

void foo(T : A[], A)(auto ref T arg)
if(is(A == float))
{
        ...
}

Array may be static or dynamic. But the length of the array must be 16 or 32. How can i test length value?

If the array can be dynamic, then the only possible way to check the array's length is with a run-time check:

if (arg.length != 16 && arg.length != 32) assert(false, "array must be 16 or 32");

That said, you can optimize both paths:

static if (isStaticArray!T)
static assert (arg.length != 16 && arg.length != 32, "errror"); //optimized compile time check
else
    assert (/+same as above+/); //Run-time check

Depending on what you want, you can also integrate the compile time check into a conditional.

----
Note that with "T : A[]", you will accept types that aren't actually arrays, but castable to. Why not use:
"isArray!T && is(ElementType!T == float)"

Also, note your template will not work on immutable(float)[]. May or may not be what you want. Just saying.

Reply via email to