1

As stated at cppreference (emphasis mine):

The value of std::numeric_limits::digits is the number of digits in base-radix that can be represented by the type T without change. For integer types, this is the number of bits not counting the sign bit and the padding bits (if any). For floating-point types, this is the number of digits in the mantissa.

It is possible for an implementation to add padding bits to the fundamental types. Is there any implementation where any of these types are actually padded? Is this wording necessary to support all hardware or is this just the standard being paranoid?

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Mestkon
  • 3,532
  • 7
  • 18
  • 1
    Are you interested in integer or any fundamental types? `std::nullptr_t` consists entirely of padding bits, AFAIK. – Language Lawyer Jun 28 '19 at 22:28
  • Integer types are certainly more interesting, but `std::nullptr_t` is a valid point. – Mestkon Jun 28 '19 at 22:39
  • [About padding bits in `nullptr_t`](https://reviews.llvm.org/D62825#1542301) – Language Lawyer Jun 28 '19 at 22:52
  • 1
    You might want to look up options, supported by some compilers, related to "packing" or "packing level". This can affect fundamental types separately from their natural alignment, not just compound/struct types. If the "packing level" is larger than their natural alignment, the organisation of an array of (say) 32 bit integers will be affected - natural alignment of 4, if packing level is 7, then each 32 bit integer in an array is placed on an 8-byte boundary (7 rounded to next multiple of 4). This means additional padding of that type. – Peter Jun 29 '19 at 01:05

1 Answers1

1

std::numeric_limits<bool>::digits returns 1, indicating bool is padded (typically 7 of 8 bits).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    But `static_assert(std::has_unique_object_representations_v);` does not fire... – Language Lawyer Jun 28 '19 at 23:32
  • From [P1236R1](http://wg21.link/P1236R1): _`bool` is specified to have some integral type as its underlying type, but the presence of padding bits for "`bool`" will remain unspecified_ o_O – Language Lawyer Jun 28 '19 at 23:39
  • @LanguageLawyer: `has_unique_object_representations_v` being true seems like a defect to me. Well spotted. – John Zwinck Jun 28 '19 at 23:52