'How does std::numeric_limits<unsigned int>::max() work
I recently learnt that:
The correct way to get the largest possible value of a numeric type on your system is to use std::numeric_limits. In particular, we can use
std::numeric_limits<unsigned int>::max()etc.
When reading the documentation of std::numeric_limits, I found that std::numeric_limits is a class template. My question is: How does std::numeric_limits find the type-/system-dependent largest/smallest value? I mean, how is that class template implemented?
In certain cases, I have seen that cppreference has a section labeled "Possible implementation" but, in the case of std::numeric_limits there is no such section.
I know that it will vary across platforms, but I just want to see one possible case to get an idea of how it is working and if is there a way to find the maximum/minimum value for a given type/system manually.
Solution 1:[1]
How does
std::numeric_limitsfind the type-/system-dependent largest/smallest value? I mean, how is that class template implemented?
Nothing is "found". std::numeric_limits is simply instantiated differently for different types. Have a look at its instantiation for int, within the GNU implementation of the C++ standard library.
The instantiation values are such that, on the target platform, they are valid.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | einpoklum |
