32

I'm seeking for a macro representing the maximum value of uint64_t as UINT_MAX is for unsigned int. i.e. I need this value to guaranteed to be (1<<64)-1.

I tried to use UINT64_MAX, but compiling with g++ results in:

'UINT64_MAX' was not declared in this scope

It's worth to mention that I have this line #define __STDC_LIMIT_MACROS in the code before using UINT64_MAX.

I was surprised to not find helpful information around the web about it.

pmr
  • 58,701
  • 10
  • 113
  • 156
Subway
  • 5,286
  • 11
  • 48
  • 59
  • idk what `#define __STDC_LIMIT_MACROS` is, but you probably want it before the includes, not before use of `UINTwhatever` – Luchian Grigore Apr 30 '13 at 11:21
  • C++11 implementations ship with `cstdint` which provides `UINT64_MAX`. Boost also provides an implementation of this header, if it is available to you. – pmr Apr 30 '13 at 11:25
  • 7
    Why macro in c++? Doesn't `std::numeric_limits::max()` work? – Igor R. Apr 30 '13 at 11:28
  • 1
    @IgorR. I thought the same, but there is no hard requirement on an implementation to provide a `numeric_limits` specialization for those types, although chances are good that there is one. Although, `max()` is less useful without `constexpr`. – pmr Apr 30 '13 at 11:35
  • @LuchianGrigore, correct. I have it before the relevant include. – Subway Apr 30 '13 at 11:48
  • @pmr, I'm linked with the Boost libraries. Do you know to specify the macro/constant that I need? – Subway Apr 30 '13 at 11:49
  • 1
    @IgorR, `std::numeric_limits::max()` did the trick. Thanks. You can put it as an answer if you want to. – Subway Apr 30 '13 at 11:53
  • @pmr The Standard requires `numeric_limit::max()` to be `constexpr`. But you're right in that some popular implementations are non-conforming. – Igor R. Apr 30 '13 at 11:57
  • @IgorR. Yes, I'm not sure which compilers OP wants to support and this has a large bearing on those answers. I tried reflecting that in my answer. – pmr Apr 30 '13 at 11:58
  • There's always `#ifndef UINT64_MAX #define UINT64_MAX 18446744073709551615` , with the relevant suffix (if `ull` is not supported, then perhaps `ui64`) – M.M Apr 08 '14 at 00:15
  • Or `#define UINT64_MAX = 0xFFFFFFFFFFFFFFFFULL`. – Big McLargeHuge Mar 02 '18 at 20:46

1 Answers1

29

Using the cstdint header portably can be quite a challenge (it is missing from some MSVC implementations). At the same time numeric_limits::max() can be hard to use without constexpr and it is not actually required to work with uint64_t. If you don't care about those things too much, std::numeric_limits<uint64_t>::max() will most likely do the trick.

Boost.Integer has an implementation of cstdint and comes with an extra traits class to get a constant maximal value. A compliant implementation of cstdint should also provide the macro UINT64_MAX, but I'm not sure about boost.

pmr
  • 58,701
  • 10
  • 113
  • 156