Subject: Re: [boost] [Review] Boost.Endian mini-review
From: Peter Dimov (lists_at_[hidden])
Date: 2015-01-26 10:11:20
Beman Dawes wrote:
> My expectation is that the class templates will only be used directly is
> in highly generic code where specifying the integer type is not a problem.
Your expectation is incorrect. Some file formats support both little and big
endian variants (to make it easier for writers, at readers' expense). It's
typical to do
read( signature );
if( signature == 0x12345678 )
read_header<little_endian>( h );
else if( signature == 0x78563412 )
read_header<big_endian>( h );
// not a valid file
which is "lowly generic".
That said, I support your decision to have both the type and the size as
template parameters; as I demonstrated in my example, the type is decided by
the program, and the size is decided by the file format, so in general, the
two do not necessarily match.
However, this allows me to sneak in another comment. Currently, if I try to
put 0x123456 into a 16 bit buffer, it silently cuts it to 0x3456 without
missing a beat. This is sometimes, but very rarely, what we want. Typically,
what one wants in this case is an exception, because this usually happens
when writing a file, and if one tries to write 0x123456 and only 0x3456
comes out, it's likely that the result will be corrupted and unreadable.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk