C++ Logo

sg10

Advanced search

[SG10] Example for __cpp_lib_byte

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Fri, 20 Oct 2017 18:46:59 +0100
Would something like this be suitable?

#if __cpp_lib_byte >= 201603
  using byte_type = std::byte;
#else
  using byte_type = unsigned char;
#endif
  alignas(T) byte_type rawbytes[sizeof(T)];
  // ...

Both forms will work, but the std::byte version might be preferred to
ensure that no arithmetic operations are accidentally performed on the
values. The desired type-safety would only be enforced on
implementations that support std::byte, but if the rest of the code is
the same you can be fairly confident there's no arithmetic even when
the type is unsigned char.

Received on 2017-10-20 19:47:22