I have C++11 code that is failing to compile because an overloaded function is redefined with the same types as arguments:
char const* foo(uint64_t) { return "%" PRIu64; }
char const* foo(unsigned long long int) { return "%llu"; }
Is there a compiler macro I can add to check equality between these two primitives, and then excise the second if it is equivalent, before compilation?
There are other functions that return character pointers for other types. For instance, adding this does not cause me any trouble, even though a signed and an unsigned long long int use the same number of bytes:
char const* foo(long long int) { return "%lld"; }
So it seems insufficient to check how much memory a type uses. What are other approaches?
You can inspect the maximum values of these type using the 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
It would probably be a better long-term solution however to create a system using templates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With