I was surprised that std::numeric_limits<std::byte>::digits
returns 0
, when I was expecting 8
. I believe this is due to lacking specialization for std::byte
hence defaulting to the, well, default implementation that return 0
in this case.
Since the standard doesn’t seem to do it, is it safe to specialize std::numeric_limits
for std::byte
myself for my code?
10
It is not safe, in fact it is not allowed. [namespace.std]/2 states
Unless explicitly prohibited, a program may add a template specialization for any standard library class template to namespace std provided that
- the added declaration depends on at least one program-defined type, and
- the specialization meets the standard library requirements for the original template.155
and it is the first bullet point that disallows this. std::byte
is a library type, not a program-defined type.
What you can do is use underlying_type_t<std::byte>
as that will be a built in arithmetic type. That will give you:
std::numeric_limits<std::underlying_type_t<std::byte>>::digits
5