Search code examples
c++boostnumeric-limits

numeric_limits is wrong on a BOOST_STRONG_TYPEDEF


I was supposing that numeric_limits::max() on a BOOST_STRONG_TYPEDEF defined type would give the same result than on the underlying type. But the following program shows that it is not the case (compiled with g++ or clang++ with boost 1.65):

#include <boost/serialization/strong_typedef.hpp>
#include <limits>
#include <iostream>

BOOST_STRONG_TYPEDEF(uint64_t, s);

int main(int , char **)
{
  std::cerr << std::numeric_limits<uint64_t>::max() << std::endl 
            << std::numeric_limits<s>::max() << std::endl;
  return 0;
}

Result:

$ clang++ test.cpp
$ ./a.out
18446744073709551615
0

Is it the expected result?


Solution

  • I was supposing that numeric_limits::max() on a BOOST_STRONG_TYPEDEF defined type would give the same result than on the underlying type.

    There's no reason to suppose that.

    You've literally asked for a new type. That's what it means to have a strong typedef. That's why you used BOOST_STRONG_TYPEDEF instead of, well, typedef.

    std::numeric_limits does not, cannot, and should not provide meaningful information for types of which it is unaware, like the new ones you've created.