On Friday, 9 February 2018 at 15:05:33 UTC, Ralph Doncaster wrote:
This seems odd to me. Is there a way I can make a function that takes an array of any type but only of a specific size in bytes?


void.d(8): Error: function void.foo (void[12] arr) is not callable using argument types (uint[3])
Failed: ["/usr/bin/dmd", "-v", "-o-", "void.d", "-I."]
void foo(void [12] arr)
{
}

void main()
{
    uint[3] arr;
    foo(arr);
}

void has no size, so what does it mean to have 12 of them?

Here are a couple of options, depending on what you need.

====
import std.stdio;

enum numBytes = 12;
void foo(T, size_t N)(T[N] arr)
    if((N * T.sizeof) == numBytes)
{
        writeln(arr);
}

void bar(ubyte[12] arr)
{
    writeln(arr);
}

void main()
{
    uint[3] arr = [20, 10, 1];
    foo(arr);
    bar(cast(ubyte[12])arr);
}
===

Output:
[20, 10, 1]
[20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0]

Reply via email to