digitalmars.D.learn - template function. test array parameter length
- ref2401 (10/10) Jan 05 2013 I have a template function that must work with an array of float
- monarch_dodra (19/29) Jan 05 2013 If the array can be dynamic, then the only possible way to check
- ref2401 (1/1) Jan 05 2013 thanks)
- Philippe Sigaud (38/47) Jan 05 2013 For a dynamic array, its type is T[] and the length is a known only at
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?
Jan 05 2013
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.
Jan 05 2013
On Sat, Jan 5, 2013 at 10:50 PM, ref2401 <refactor24 gmail.com> 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?
For a dynamic array, its type is T[] and the length is a known only at
runtime (by definition). You can test it with a runtime test, but not with
a template constraint.
For a static array T[n], n is part of the type so you can indeed filter the
bad ones during compilation.
Using std.traits and std.range:
import std.array;
import std.traits;
import std.range;
void foo(T)(auto ref T arg)
if ( isDynamicArray!T && is(ElementType!T == float)
|| isStaticArray!T && (T.length == 16 || T.length == 32) &&
is(ElementType!T == float))
{}
void main()
{
float[] dynamicArr;
float[3] staticArr1 = [0.0,1.0,2.0];
float[16] staticArr2;
foo(dynamicArr);
//foo(staticArr1); // fails
foo(staticArr2);
}
Or, if you prefer using the is() expression:
void bar(T)(auto ref T arg)
if ( is(T _ == U[], U)
|| is(T _ == U[n], U, size_t n) && (n == 16 || n == 32))
{}
void main()
{
float[] dynamicArr;
float[3] staticArr1 = [0.0,1.0,2.0];
float[16] staticArr2;
bar(dynamicArr);
//bar(staticArr1); // fails
bar(staticArr2);
}
Jan 05 2013









"ref2401" <refactor24 gmail.com> 