0
UINT64_MAX

is defined in <cstdint>: https://en.cppreference.com/w/cpp/header/cstdint.

This is what I've always used. However, I see C++ provides this alternative as well:

std::numeric_limits<uint64_t>::max()

(see: https://en.cppreference.com/w/cpp/types/numeric_limits/max), which is part of the many numeric limits options defined in the <limits> header: https://en.cppreference.com/w/cpp/types/numeric_limits.

The C++ version seems overly verbose to me and appears to have no benefits. I assume it's included simply to be thorough, since they were adding additional functionality via other numeric_limits<> created in C++, but wanted to cover everything, I imagine, while they were at it.

Is there any benefit of using the C++-only version (std::numeric_limits<uint64_t>::max()) over the C or C++ version (UINT64_MAX)?

I'd really prefer the UINT64_MAX version, but in general find a lot of pushback from hard-core C++ developers whenever they see me write something in C++ which also would be valid in C. :)

Note: I haven't had pushback on this one yet, but I'd like some insight first to see if there is something I'm missing which truly makes one version better than the other.


Update: here's my own answer:

I'm not too happy this question was closed literally in the very same second as I pressed the submit button to add my own answer, but here's my own answer to this question:

As @Bob__ pointed out in the comments under the question, std::numeric_limits<uint64_t>::max() is really preferred when writing a C++ template where the uint64_t part needs to be replaced with the typename T.

However, if not in a template, it seems to me UINT64_MAX would be preferred since it's short, straight-to-the-point, less verbose, and easier to read.

Gabriel Staples
  • 22,024
  • 5
  • 133
  • 166
  • 3
    Really stoke the fire and use `-1ull` instead ;) – NathanOliver Sep 23 '20 at 19:28
  • 3
    Well, which one would you prefer when writing a template? – Bob__ Sep 23 '20 at 19:29
  • @NathanOliver, haha. I love it. I prefer the more explicit `(uint64_t)0 - 1`, however, which emphasizes the type and the underflow all at once. I've used ths quite a few times, actually. – Gabriel Staples Sep 23 '20 at 19:30
  • 2
    As @Bob__ mentioned, if you use the `` variant in generic code, it'll end up more verbose. – Mansoor Sep 23 '20 at 19:31
  • @Bob__, great point: the `std::numeric_limits<>` version is perfect inside a template, if that were the case, whereas the cstdint version can't easily be changed to match the templated type. – Gabriel Staples Sep 23 '20 at 19:31

0 Answers0