The standard does not provide a guarantee that the standard integer types are the only integer types supported by the compiler. Indeed, the C and C++ standards explicitly give permission to compilers to define other integer types, collectively known as "extended integer types":
There may also be implementation-defined extended signed integer types.
Likewise, for each of the extended signed integer types there exists a corresponding extended unsigned integer type ...
the extended signed integer types and extended unsigned integer types are collectively called the extended integer types.
And the C standard does not prohibit an implementation from using extended integer types to implement the stdint.h
types. There is even this non-normative notation, just to make it clear:
Some of these types may denote implementation-defined extended integer types.
If you want a function that can take any integer type, you have two options: make it a template (probably using SFINAE to disallow non-integral types) or provide a single overload that takes std::intmax_t
. The standard requires that intmax_t
is the largest integer supported:
designates a signed integer type capable of representing any value of
any signed integer type
And of course "signed integer type" includes standard and extended types. So std::intmax_t
can handle any signed integer that the implementation supports.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…