I just created a bug by testing a value against the following expression:
std::numeric_limits<decltype(allocationCount)>::max()
In this context, allocationCount is a std::atomic<std::size_t>.
Apparently, the above mentioned expression compiles and evaluates to 0 on both Clang 10 and GCC 10:
#include <atomic>
#include <cstdint>
#include <limits>
#include <string>
static std::atomic<std::size_t> allocationCount = 0;
uint64_t buggyGetMax() {
return std::numeric_limits<decltype(allocationCount)>::max();
}
uint64_t correctGetMax() {
return std::numeric_limits<decltype(allocationCount)::value_type>::max();
}
What I meant to use was
std::numeric_limits<decltype(allocationCount)::value_type>::max()
that produces the value I wanted, that is std::numeric_limits<std::size_t>::max().
The question I have is why std::numeric_limits<decltype(allocationCount)> even compiled? Shouldn't it fail as std::numeric_limits<std::string> does?
If this is by design, why is the max() 0?