You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++函数重载编译期规则咨询:面试遇到的重载匹配问题

Hey there! Let's break down exactly what's happening with your C++ function overload matching here—this is such a common point of confusion, so you’re definitely not alone in scratching your head over it. Let’s start with the core logic the compiler uses to pick the right overload, then walk through your specific calls step by step.


How the C++ Compiler Picks Overloads (In Plain English)

When you call an overloaded function, the compiler works through three tiers of checks to find the best possible match:

  • Exact Match: First, it looks for a function where your argument types line up perfectly with the parameters (this includes tiny, safe tweaks like turning an array into a pointer or adding a const qualifier).
  • Promotion: If no exact match exists, it checks for functions where your arguments can be promoted to the parameter types. Promotions are "safe" conversions that don’t lose data—think char → int, float → double, or short → int.
  • Standard Conversion: If promotions don’t work, it falls back to standard conversions. These are conversions that might lose information or precision, like double → int (truncating decimals), int → long, or double → float.

If multiple functions land in the same tier, the compiler then compares the conversion sequences for each argument. A candidate wins if:

  1. For every argument, its conversion is no worse than the other candidates' conversions, AND
  2. At least one argument’s conversion is better than the others.

If no candidate meets this bar, you’ll get a compiler error for an ambiguous overload.


Let’s Apply This to Your Code

First, here’s your overload set for easy reference:

int mult(int a, int b) { cout<<"int "; return a*b; }
long mult(long a, long b) { cout<<"long "; return a*b; }
float mult(float a, float b) { cout<<"float "; return a*b; }

First Call: mult(5.2, 7)

Your arguments here are a double (5.2) and an int (7). Let’s evaluate each overload:

  • mult(int, int):
    • 5.2 (double) → int: Standard conversion (truncates the decimal part, losing info)
    • 7 (int) → int: Exact match
  • mult(long, long):
    • 5.2 (double) → long: Standard conversion (truncates the decimal)
    • 7 (int) → long: Standard conversion (widens the int to long—no data loss, but still a conversion, not a promotion)
  • mult(float, float):
    • 5.2 (double) → float: Standard conversion (may lose precision since double holds more bits than float)
    • 7 (int) → float: Standard conversion (converts the integer to a floating-point value)

Now, let’s compare these candidates:
mult(int, int) has an exact match for the second argument, which is better than the standard conversions required for the second argument in the other two overloads. Even though the first argument requires a standard conversion (same as the others), the exact match on the second argument makes this the best candidate. The compiler will pick mult(int, int) here, outputting "int " and returning 5*7=35 (since 5.2 is truncated to 5).

Second Call: mult(5, 7.2)

Your arguments are an int (5) and a double (7.2). Let’s break down each overload:

  • mult(int, int):
    • 5 (int) → int: Exact match
    • 7.2 (double) → int: Standard conversion (truncates to 7)
  • mult(long, long):
    • 5 (int) → long: Standard conversion
    • 7.2 (double) → long: Standard conversion
  • mult(float, float):
    • 5 (int) → float: Standard conversion
    • 7.2 (double) → float: Standard conversion

Just like the first call, mult(int, int) has an exact match for one argument (the first one here), which beats the standard conversions needed for that argument in the other overloads. So the compiler will again pick mult(int, int), outputting "int " and returning 5*7=35 (7.2 is truncated to 7).


What If There Was a Tie?

Suppose you added an overload like mult(double, int)—that would be an exact match for both arguments in your first call, so it would win hands down. But if you had two overloads where each had an exact match for one argument (e.g., mult(int, double) and mult(double, int)), calling mult(5, 7.2) would result in an ambiguous overload error, since neither candidate is clearly better than the other.


内容的提问来源于stack exchange,提问作者Michael B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:07:42