27

I need to know if intmax_t is always "the same type" as uintmax_t except using two's complement instead of unsigned value.

Or putting this in formal terms, will the code below always compile in a standard-compliant compiler?

#include <cstdint>

// The important assertion:
static_assert(sizeof(std::uintmax_t) == sizeof(std::intmax_t));
// Less important assertions:
static_assert(UINTMAX_MAX == static_cast<std::uintmax_t>(INTMAX_MAX) * 2 + 1);
static_assert(-static_cast<std::intmax_t>(UINTMAX_MAX / 2) - 1 == INTMAX_MIN);

I'm particularly interested in C++17.

I know that C++20 is the first version of the standard that enforces two's complement but the size of the variable is more important to me than the representation.

0

3 Answers 3

29

Yes, uintmax_t is guaranteed to be the unsigned counterpart of intmax_t.

From the C standard (N1570 7.20.1):

When typedef names differing only in the absence or presence of the initial u are defined, they shall denote corresponding signed and unsigned types as described in 6.2.5; an implementation providing one of these corresponding types shall also provide the other.

(C++ simply refers to C for the description of C standard library headers.)

6.2.5 p6:

For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements.

C++ is similar to C in this regard ([basic.fundamental]/3):

For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type [...] which occupies the same amount of storage and has the same alignment requirements as the corresponding signed integer type; that is, each signed integer type has the same object representation as its corresponding unsigned integer type. Likewise, for each of the extended signed integer types there exists a corresponding extended unsigned integer type with the same amount of storage and alignment requirements.

Which means intmax_t and uintmax_t always have the same size.

However, it is not guaranteed that the other two assertions will hold (prior to C++20/C23).

7
  • 1
    Even if the types are guaranteed to be 2's complement, the standard doesn't rule out one having more padding bits and fewer value bits in the object representation. They only guarantee the object representation is the same size, but that includes possible padding bits. (Unless there are some other relevant rules.) Jan 23 at 19:10
  • 1
    I’m not exactly sure if this is exactly what @PeterCordes wrote, but C99 (unlike the current C23 draft) explicitly permits a scenario in which the corresponding types can still differ, as long as they use the same amount of storage/alignment (in bytes); for example, it’s possible to have 31-bit unsigned (0‥2³¹-1) and 32-bit signed (-2³¹‥2³¹-1), or 32-bit unsigned (0‥2³²-1) and 28-bit signed (-2²⁷‥2²⁷-1), in the corresponding types.
    – mirabilos
    Jan 23 at 23:46
  • @mirabilos: Yes, that's exactly the kind of possibility I was talking about. But C23 will disallow that? How, with what wording, so we can check for similar wording in the ISO C++ standards (since this is a C++ question)? Jan 23 at 23:57
  • 1
    @PeterCordes the whole M ≤ N thing is gone from n3047: “If the corresponding unsigned type has width N, the signed type uses the same number of N bits, its width, as value bits and sign bit.”
    – mirabilos
    Jan 25 at 5:44
  • 2
    @mirabilos: Thanks, yeah that'd do it. Requiring both signed and unsigned types to have the same number of value bits, and defining the value-range as 2's complement or unsigned, does fully close the loophole. There can still be padding bits, except in unsigned char, and the optional fixed-width types like uint64_t / int64_t, but not a difference between signed/unsigned. (So signed char also can't have padding now.) Jan 25 at 6:19
11

From the comments/title, it seems you're asking if they're required to be the same size; in which case, the short answer is yes ... long answer ... kind of :)

Quote from [basic.fundamental]/3 (C++17 draft N4713)

For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: “unsigned char”, “unsigned short int”, “unsigned int”, “unsigned long int”, and “unsigned long long int”, each of which occupies the same amount of storage and has the same alignment requirements as its corresponding signed integer type.

(emphasis mine)

This guarantees that the unsigned versions take up the same size as their signed equivalents.

That being said, the standard [cstdint.syn] only states:

using intmax_t = signed integer type;

using uintmax_t = unsigned integer type;

The [basic.fundamental]/2 states

The standard and extended signed integer types are collectively called signed integer types.

and The [basic.fundamental]/3 states

The standard and extended unsigned integer types are collectively called unsigned integer types

So, technically, a compiler does not have to implement them as the same type, since that's an implementation detail; practically speaking though, they'd be the same.


As noted by duck, the C standard does indicate that there must be corresponding versions between types with u and no u prefix. The C standard is referenced via [cstdint.syn]/2

2
  • In what you quote I don’t see a requirement that extended integer types have corresponding signed/unsigned versions. Just from this I could still imagine that for example $intmax_t$ is an extended integer type with no corresponding unsigned version and $uintmax_t$ is a smaller standard integer type.
    – Carsten S
    Jan 23 at 8:27
  • 1
    @CarstenS, true but I didn't copy the full quote. It does go on to say, in the same paragraph, that the same applies to extended signed integer types.
    – ChrisMM
    Jan 23 at 19:23
4

My answer was shown to be incorrect by duck. Thanks, duck!

https://stackoverflow.com/a/75203111/2027196

My original answer for future reference:

uintmax_t and intmax_t are not guaranteed by the standard to be the same width, but there is no system with a modern C compiler for which they will be different widths. There may not even be a non-modern C compiler for which they will be different widths (asserted without evidence in the same vein as "the sky is blue" type arguments).

The best you can get is that uintmax_t and intmax_t are guaranteed by convention to be the same width. I say "best you can get" but I'd be willing to rely on this guarantee more often than I rely on a compiler perfectly implementing all of its edge-case SFINAE requirements or similar.

Put your static_assert at the top of your library (maybe in an assumptions.hpp file) and then never worry about this problem ever again.

7
  • There are some virtual machines where the largest signed type is bigger than the largest unsigned type. In the JVM, for example, the largest signed type is 64 bits while the largest unsigned type is 16 bits. Because C99 requires that implementations support an unsigned 64-bit type, a C implementation targeting the JVM would be required to include its own logic to perform long division for divisor values of 2**63 and larger. If there were a VM that supported 128-bit signed arithmetic but not unsigned, I woudln't think it implausible that someone writing a C implementation for it might.,..
    – supercat
    Jan 23 at 16:16
  • ...want to support 128-bit signed types but only 64-bit unsigned types. Also as a historical note, a 2005 compiler for a ones'-complement Univac supported 71-bit signed values but only 36-bit unsigned values (IMHO, C99 would have been practically implementable on that platform if support for an unsigned type of 64 bits or larger was highly recommended but optional, but C99's mandated support for that type limited the platform to "C89 plus extensions").
    – supercat
    Jan 23 at 16:18
  • @supercat that's all very interesting and I appreciate the history. None of that counters duck's answer, though -- does it? Jan 23 at 18:31
  • There are many sitations where the Standard would forbid a compiler from processing a program in the fashion that would be most useful for the program's intended purpose, and compilers often have options to select "not quite conforming" modes. If a platform's target could practically support a signed type which was bigger than the largest unsigned type it could support, an implementation for such a platform may offer a "conforming" mode which simply disables support for that type, and a "non-conforming" mode which adds support in ways the Standard wouldn't allow.
    – supercat
    Jan 23 at 21:40
  • Okay, but all standard-compliant compilers would have intmax_t and uintmax_t as the same width. Got it. Jan 24 at 2:50

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.