Compiler macro to check difference between uint64_t and unsigned long long int

I have C ++ 11 code that doesn't compile because the overloaded function is overridden with the same types as the arguments:

char const* foo(uint64_t) { return "%" PRIu64; }
char const* foo(unsigned long long int) { return "%llu"; }

      

Is there a compiler macro that I can add to check for equality between the two primitives and then repeat the second one, if equivalent, before compiling?

There are other functions that return character pointers for other types. For example, adding this does not cause any problems, even if signed

both unsigned long long int

use the same number of bytes:

char const* foo(long long int) { return "%lld"; }

      

Thus, it is not enough to check how much memory a type is using. What other approaches are there?

+3


source to share


1 answer


You can check the maximum values โ€‹โ€‹of this type using definitions from climits

and cstdint

:

#include <climits>
#include <cstdint>

char const* foo(uint64_t) { return "%" PRIu64; }

//ULLONG_MAX defined in climits, UINT64_MAX in cstdint
#if ULLONG_MAX != UINT64_MAX

char const* foo(unsigned long long int) { return "%llu"; }

#endif

      



Perhaps the best long-term solution would be to create a system using templates.

+2


source







All Articles