Title
`std::byteswap` can make sense for some types with padding bytes
Status
new
Section
[bit.byteswap]
Submitter
Jiang An

Created on 2026-05-21.00:00:00 last changed 1 week ago

Messages

Date: 2026-05-24.09:51:11

Proposed resolution:

This wording is relative to N5046.

  1. Modify [bit.byteswap] as indicated:

    template<class T>
      constexpr T byteswap(T value) noexcept;
    

    -1- Constraints: `T` models `integral`.

    -2- Mandates: `T` does not have padding bits ([basic.types.general])For each byte in the object representation of `T`, bits in the byte are either all padding bits ([basic.types.general]) or all non-padding bits.

    -3- Let the sequence R comprise the bytes of the objectvalue representation of value in reverse order.

    -4- Returns: An object `v` of type `T` such that each byte in the objectvalue representation of `v` is equal to the byte in the corresponding position in R.

Date: 2026-05-21.00:00:00

Currently, `std::byteswap` doesn't accept types with padding bits. The prohibition might be a bit too strict. E.g., if the implementation provides `__int48` such that `sizeof(__int48) == 8` and there're 2 padding bytes in the object representation of `__int48`, `std::byteswap` can arguably make sense for `__int48` if we specify its semantics as reversing byte order of the value representation instead of the object representation. Some implementors (see llvm/llvm-project#196512) think that it's meaningful make `std::byteswap` support `__int48` and its friends.

The issue was discovered when making `std::byteswap` support `_BitInt` in libc++. While `_BitInt` is being standardized in C++ via P3666R4, libc++ is trying to recognize (`unsigned`) `_BitInt(N)` as extended integer types and achieve full library support.

History
Date User Action Args
2026-05-24 09:51:11adminsetmessages: + msg16324
2026-05-21 00:00:00admincreate