Search code examples
c++overloading

Function overloading in C++ with int and int64_t. Ambiguous match with "1LL"


#include <iostream>
void hi(int x) {
    std::cout << "hi\n";
}
void hi(int64_t y) {
    std::cout << "hi2\n";
}
int main() {
    hi(3LL);
}

Why does it throw ambiguous match? I think it should resolves to the second function since it's an exact match. Or is int64_t a different type from 3LL?

It seems that the error only occurs on some compiler, namely g++ but not clang. Not sure if it is caused by different compiler flags or version used though.


Solution

  • Or is int64_t a different type from 3LL?

    Yes, int64_t is long (in this implementation). 3LL is long long.

    Modern IDE have a "go to definition" feature, where you can position your cursor over int64_t and go to https://github.com/lattera/glibc/blob/master/bits/stdint-intn.h#L27 and then go to https://github.com/lattera/glibc/blob/master/posix/bits/types.h#L43 .