JWWalker
JWWalker

Reputation: 22717

Why do 'long' and 'long long' types behave differently when both are 64 bits?

I'm compiling for the x86_64 architecture using Apple Clang (Xcode 14). In the following code:

#include <iostream>
#include <stdint.h>

static void foo( long v )
{
    std::cout << v;
}

static void foo( unsigned long v )
{
    std::cout << v;
}

int main(int argc, const char * argv[]) {
    static_assert( sizeof(long) == sizeof(long long), "wrong size");
    long i = 3;
    foo( i );
    long long j = 3;
    foo( j );
    return 0;
}

This produces a compile error, pointing to the second call to foo:

error: call to 'foo' is ambiguous

The first call does not produce an error, and the assertion does not fail, verifying that in this case long and long long are both 64-bit (signed) integers.

So, why do these two types behave differently with regard to overload resolution?

Upvotes: 0

Views: 72

Answers (0)

Related Questions