[libcxx-commits] [libcxx] [libc++] std::byteswap support for _BitInt(N) (PR #196512)

Xavier Roche via libcxx-commits libcxx-commits at lists.llvm.org
Sun May 10 08:36:24 PDT 2026


================
@@ -23,27 +24,34 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 #if _LIBCPP_STD_VER >= 23
 
 template <integral _Tp>
+  requires(sizeof(_Tp) == 1 || (numeric_limits<_Tp>::digits + numeric_limits<_Tp>::is_signed) % 16 == 0)
----------------
xroche wrote:

After digging,
- The pros: would be standard conformant (Constraints: T models integral.; Mandates: T does not have padding bits.)
- The cons: as far as I can see, libstdc++ and MSVC accept them

References:

gcc/libstdc++-v3/include/std/bit, lines 135-150 (master branch):                                                                                                          
```c++
  template<integral _Tp>
    [[nodiscard]]                                                                                                                                                                       
    constexpr _Tp                                           
    byteswap(_Tp __value) noexcept
    {                             
      if constexpr (sizeof(_Tp) == 1)                                                                                                                                                   
        return __value;              
      ...                                                                                                                                                                               
    }                                                       
```

STL/stl/inc/bit, lines 103-122 (main branch):                                                                                                                              
```c++
  template <class _Ty>                                                                                                                                                                  
      requires is_integral_v<_Ty>                                                                                                                                                       
  _NODISCARD constexpr _Ty byteswap(const _Ty _Val) noexcept {
      if constexpr (sizeof(_Ty) == 1) {                                                                                                                                                 
          return _Val;                                                                                                                                                                  
      } else if constexpr (sizeof(_Ty) == 2) {
```

Unsure which path would be the best ? We could add a non-conformance warning comment also but hmmm.



https://github.com/llvm/llvm-project/pull/196512


More information about the libcxx-commits mailing list