C++ Logo

std-proposals

Advanced search

Make non-specialized numeric_limits<T> use ill-formed

From: Thomas Mercier <thomas.mercier.jr_at_[hidden]>
Date: Fri, 10 Apr 2020 11:27:19 -0700
Hi,

I encountered some surprising behavior from the std::numeric_limits<T>
class template when experimenting with std::byte. The integer
representation of the maximum std::byte value is 0 according to
std::numeric_limits<std::byte>::max(). That is because there is no
specialization of std::numeric_limits<T> for std::byte, because std::byte
is not an arithmetic type. Ok, fine. But the fact that the program
compiles, and produces an unexpected value is worrisome!

The standard specifies that "The default numeric_­limits<T> template shall
have all members, but with 0 or false values." (
https://eel.is/c++draft/numeric.limits#3)

I would prefer to see this say something like, "a program which uses the
default numeric_limits<T> template is ill-formed", so that an error is
produced at compile time rather than a value initialized result (
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/limits#L321
 and
https://github.com/llvm/llvm-project/blob/master/libcxx/include/limits#L150
).

Does anyone know why the current wording specifies "0 or false values", or
what any objections to my suggested change might be?

https://godbolt.org/z/YVSECn

Thanks,
T.J.

Received on 2020-04-10 13:30:26