Passing a ubyte[] to a function taking a 'ref ubyte[16]'

Ali Çehreli acehreli at yahoo.com
Sat Jun 23 23:39:00 PDT 2012


On 06/23/2012 04:51 PM, Jonathan M Davis wrote:

 >> This seems to work, but it's very ugly:
 >> ------------
 >> _digest.finish(*cast(ubyte[16]*)buf.ptr);
 >> ------------
 >>
 >> I thought this might create a temporary, but it passes all unittests,
 >> so it seems to work?
 >
 > I believe that that will work, but it's definitely ugly. However, if 
you do
 > that, you _need_ to put an assertion about the length of buf in there,
 > otherwise, you could be using memory from past the end of buf.
 >
 > - Jonathan M Davis

I had the following function template in a message that did not appear 
on the newsgroup:

ref T[N] asArray(size_t N, T)(ref T[] source)
{
     enforce(source.length >= N);
     return *cast(T[N]*)source.ptr;
}

// ...

     foo(asArray!16(b));

Ali



More information about the Digitalmars-d-learn mailing list