C++ Logo

std-discussion

Advanced search

Re: What even happened to <net> and byte swapping?

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 26 Feb 2021 10:33:04 -0500
On Fri, Feb 26, 2021 at 10:08 AM Edward Catmur via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> On Fri, Feb 26, 2021 at 1:13 PM Matthew Woehlke via Std-Discussion <std-discussion_at_[hidden]> wrote:
>>
>> Overloads taking `span` might be nice, also.
>
>
> How can you convert a span of bytes without knowing the layout of the fields within it? Say you have a range of 10 bytes that is {int64, uint32, 4x int8, int16}?

Presumably, it'd look something like this:

```
span<std::byte> data = ...
data = in_place_from_le<std::int64_t>(data);
data = in_place_from_le<std::int32_t>(data);
data = in_place_from_le<std::int8_t[4]>(data);
data = in_place_from_le<std::int16_t>(data);
```

These functions return a span that has been incremented by the size of
the data type. Non-in-place versions (returning rvalues) would take
their `span`s by lvalue reference and offset them directly or return a
struct containing the value and the offsetted `span`. I would prefer
the lvalue reference version.

>And what if there's IEEE floats in there?

That's just another type.

Received on 2021-02-26 09:33:18