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

Johannes Pfau nospam at example.com
Sun Jun 24 00:29:24 PDT 2012


Am Sat, 23 Jun 2012 23:39:00 -0700
schrieb Ali Çehreli <acehreli at yahoo.com>:

> 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
> 

Yes that's better. Could this be added to phobos, probably
std.typecons?



More information about the Digitalmars-d-learn mailing list