problems with std.bitmanip.append

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 25 10:09:03 PDT 2015


On Wednesday, 25 March 2015 at 15:44:50 UTC, Hugo wrote:
> Hi,
>
> I need to append an uint as an array of ubytes (in little 
> endian) to an existing array of ubytes. I tried to compile this 
> code (with dmd 2.066.1 under Windows 7 x86-64):
>
> void main() {
>    ubyte[] buffer = [0x1f, 0x8b, 0x08, 0x00];
>    import std.system;
>    import std.datetime : Clock, stdTimeToUnixTime;
>    import std.bitmanip : append;
>    
> buffer.append!ubyte(cast(uint)stdTimeToUnixTime(Clock.currStdTime), 
> Endian.littleEndian);
> }
>
> But it gives me this error: template std.bitmanip.append cannot 
> deduce function from argument types !(ubyte)(ubyte[], uint, 
> Endian)
>
> Supposedly append "Takes an integral value, converts it to the 
> given endianness, and appends it to the given range of ubytes 
> (using put) as a sequence of T.sizeof ubytes", so I thought I 
> could use it, but after reading the documentation page for the 
> function and the examples, I honestly can't understand where is 
> the problem is.
>
> Please, help!
>
> Regards, Hugo

As per the signature in the docs:

void append(T, Endian endianness = Endian.bigEndian, R)(R range, 
T value)

The endianness is the second template argument. What you need to 
write is

buffer.append!(uint, 
Endian.littleEndian)(cast(uint)stdTimeToUnixTime(Clock.currStdTime));

or

append!(uint, Endian.littleEndian)(buffer, 
cast(uint)stdTimeToUnixTime(Clock.currStdTime));

Note that you don't need to specify the third template argument, 
that will be inferred automatically from the type of `buffer`


More information about the Digitalmars-d-learn mailing list