The following code compiles fine on with Gcc 4.8.5 and Clang 3.9.1 on my CentOS system but does not compile with anything (Gcc, Clang) on my macOS system. They are both 64-bit systems. I guess that it comes from the different standard library used by those systems.
#include <iostream>
#include <cstdint>
std::int32_t f(std::int32_t a) { return 0; }
std::uint32_t f(std::uint32_t a) { return 1; }
std::int64_t f(std::int64_t a) { return 2; }
std::uint64_t f(std::uint64_t a) { return 3; }
int main() {
const std::size_t n = 0;
std::cout << f(n) << std::endl;
return 0;
}
On macOS, it complains about ambiguous overloading. I am surprised as I thought that std::size_t and std::uint64_t are the same on 64-bit systems.
>> clang++ -std=c++11 main.cpp -o main
main.cpp:12:16: error: call to 'f' is ambiguous
std::cout << f(n) << std::endl;
^
main.cpp:4:14: note: candidate function
std::int32_t f(std::int32_t a) { return 0; }
^
main.cpp:5:15: note: candidate function
std::uint32_t f(std::uint32_t a) { return 1; }
^
main.cpp:6:14: note: candidate function
std::int64_t f(std::int64_t a) { return 2; }
^
main.cpp:7:15: note: candidate function
std::uint64_t f(std::uint64_t a) { return 3; }
^
1 error generated.
Even though, this example looks stupid, I really need to overload f for signed/unsigned and 32/64-bit integers in my real application and their behaviour must depend on the size of the integer as I am playing with bits. What should I do?
Aucun commentaire:
Enregistrer un commentaire