非静的メンバ関数:暗黙を組み込んだ後
Overload resolution is a mechanism for selecting the best function to call given a list of expressions that are to be the arguments of the call and a set of candidate functions that can be called based on the context of the call. The selection criteria for the best function are the number of arguments, how well the arguments match the parameter-type-list of the candidate function, how well (for non-static member functions) the object matches the implicit object parameter, and certain other properties of the candidate function.
:
void func(double); // #1
void func(int) const; // #2
また他の引数として、オーバーロード解決([over.match]/p1)であると考えられるimplicit object parameterを受け入れますオブジェクトのパラメータをメンバ関数のシグネチャに追加すると、コンパイラは2つのオーバーロードを認識します。
void func(Base&, double); // #1
void func(const Base&, int); // #2
、コールに基づいて最適な実行可能な機能を選択しようとします:(タイプBase
の非const左辺値である)base
からBase&
への変換は、直接(完全に一致ランクを持っている
Base base;
base.func(1);
を参照結合はIdentity conversionを生じる) - Table 13を参照のこと。二番目のパラメータ、double
に不可欠prvalue 1
からの変換のための今すぐ
— S1 and S2 are reference bindings ([dcl.init.ref]), and the types to which the references refer are the same type except for top-level cv-qualifiers, and the type to which the reference initialized by S2 refers is more cv-qualified than the type to which the reference initialized by S1 refers. [ Example:
int f(const int &);
int f(int &);
int g(const int &);
int g(int);
int i;
int j = f(i); // calls f(int &)
int k = g(i); // ambiguous
:base
からconst Base&
への変換は完全一致でもあるランクは、しかし、[over.ics.rank]/p3.2.6は、より良い変換シーケンスを持っている#1
を宣言します浮動積分変換([conv.fpint])です。変換ランクです。一方、1
~int
は、のID変換であり、完全一致ランクです。この引数には、考えられている#2
がより良い変換シーケンス([over.ics.rank]/p3.2.2)持っている:
Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then
— for some argument j, ICSj(F1) is a better conversion sequence than ICSj(F2), or, if not that, [...]
:
— the rank of S1 is better than the rank of S2, or S1 and S2 have the same rank and are distinguishable by the rules in the paragraph below, or, if not that, [...]
オーバーロードの解決成功するための変換シーケンスが異なるために最大で1つのパラメータ([over.match.best])が存在することが必要です
ここで、ICS は(#1)〜(#2)ICS よりも優れているが、今度は、ICS は(#2)ICSよりも優れて(#1)であるため、コンパイラは2つのオーバーロードを選択できず、あいまいさを検出します。
整数は浮動小数点型に簡単に変換できます。 'func(int)'が最適なマッチですが、 'func(double)'は依然として実行可能な選択肢であり、呼び出しが曖昧になります。 –
constメンバ関数と非constメンバ関数を含む複雑なロジックに慣れているようです。 FWIWでは、 'func(double)'を 'const'メンバ関数に変更することで問題が取り除かれます。 –