算術促銷和轉換

將整數型別轉換為相應的提升型別比將其轉換為其他整數型別更好。

void f(int x);
void f(short x);
signed char c = 42;
f(c); // calls f(int); promotion to int is better than conversion to short
short s = 42;
f(s); // calls f(short); exact match is better than promotion to int

float 提升為 double 比將其轉換為其他浮點型別更好。

void f(double x);
void f(long double x);
f(3.14f); // calls f(double); promotion to double is better than conversion to long double

除促銷之外的算術轉換既不好也不差。

void f(float x);
void f(long double x);
f(3.14); // ambiguous

void g(long x);
void g(long double x);
g(42); // ambiguous
g(3.14); // ambiguous

因此,為了確保在使用任何標準型別的整數或浮點引數呼叫函式 f 時不會出現歧義,總共需要 8 個過載,因此對於每個可能的引數型別,要麼過載匹配確切地或者將選擇具有提升的引數型別的唯一過載。

void f(int x);
void f(unsigned int x);
void f(long x);
void f(unsigned long x);
void f(long long x);
void f(unsigned long long x);
void f(double x);
void f(long double x);