Slides for P3639R0 — The _BitInt Debate

Document number:
P3721R0
Date:
2025-06-04
Audience:
SG22
Project:
ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21
Reply-To:
Jan Schultke <janschultke@gmail.com>
Source:
github.com/Eisenwave/cpp-proposals/blob/master/src/bitint-debate-slides.cow

This document has custom controls:

  • ,  ↓ : go to the next slide
  • ,  ↑ : go to previous slide

The _BitInt Debate
P3639R0

Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 1

Introduction

C23 now has _BitInt type for N-bit integers (WG14 [N2763], [N2775]):

// 8-bit unsigned integer initialized with value 255. // The literal suffix wb is unnecessary in this case. unsigned _BitInt(8) x = 0xFFwb;
Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 2

Possible implementations

𝔽 – Fundamental type 𝕃 – Library type
template <size_t N> using bit_int_t = _BitInt(N); template <size_t N> using bit_uint_t = unsigned _BitInt(N); template <size_t N> class bit_int { private: _BitInt(N) _M_value; public: // ... }; template <size_t N> class bit_uint { /* ... */; };
Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 3

𝔽 – Fundamentals can do more

A fundamental type is needed for full C compatibility:

switch (_BitInt(32) x = 0uwb) { /* ... */ } struct S { _BitInt(32) x : 10; /* OK, _BitInt(32) bit-field */ };

A fundamental type could have deduction superpowers:

template <size_t N> void f(bit_int_t<N>); foo(0); // OK, calls foo<32> on most platforms

This could also be addressed by P2998, "Deducing function parameter types using alias template CTAD".

Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 4

𝔽 – Why not reinvent integers?

Users have many grievances with the standard integers:

decltype(+uint8_t{1}) // int?! unsigned(0) < -1 // true?! uint8_t x = 1000; // OK?! int x = 0.5f; // OK?! 0 + true // OK?! uint32_t x; // optional?! uint128_t x; // why not?! sizeof(int) == sizeof(long) // true?!
Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 5

𝔽 – Problems with a common spelling

#define _BitInt(...) std::bit_int<(__VA_ARGS__)> unsigned _BitInt(32) x; // error: applying "unsigned" to class type

⇒ Both C and C++ developers would need a macro for unsigned types.

/* C */ #define _BitUint(...) unsigned _BitInt(__VA_ARGS__) /* C++ */ #define _BitUint(...) std::bit_uint<(__VA_ARGS__)>
Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 6

𝕃 – Class is easier to implement, teach

template <size_t N> class bit_int { unsigned long long limbs[/* ... */]; public: /* constructors, operator overloads, etc. */ };
Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 7

𝕃 – Fundamentals have huge blast radius

Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 8

Summary

Key arguments (see [P3639R0] for more):

Author position: neutral / slightly 𝔽

Jan Schultke  |  Slides for P3639R0 — The _BitInt Debate  |  SG22 Telecon 2025-06-04  |  Slide 9

References

[N1744] Michiel Salters. Big Integer Library Proposal for C++0x 2005-01-13 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1744.pdf
[P1889R1] Alexander Zaitsev et al.. C++ Numerics Work In Progress 2019-12-27 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1889r1%2epdf
[N2763] Aaron Ballman et al.. Adding a Fundamental Type for N-bit integers 2021-06-21 https://open-std.org/JTC1/SC22/WG14/www/docs/n2763.pdf
[N2775] Aaron Ballman, Melanie Blower. Literal suffixes for bit-precise integers 2021-07-13 https://open-std.org/JTC1/SC22/WG14/www/docs/n2775.pdf