2013-03-23 14 views
8

以下の式return max(max(x, y), z);の中のmax(x, y)というコールのオーバーロードの解像度が、テンプレート以外の関数char const* max(char const*, char const*)を呼び出すのはなぜですか? xconst char* const&yconst char* const&であるよう以下の過負荷解決がテンプレート以外の関数を呼び出すのはなぜですか?

は、私の知る限り理解できるよう、機能max<const char*>(x, y)は、前者よりも優れたフィット感です!

#include <iostream> 

template <typename T> 
T const& max (T const& x, T const& y) 
{ 
    return x < y ? y : x; 
} 

char const* max (char const* x, char const* y) 
{ 
    return std::strcmp(x, y) < 0 ? y : x; 
} 

template <typename T> 
T const& max (T const& x, T const& y, T const& z) 
{ 
    return max (max(x, y), z); 
} 

int main() 
{ 
    const char* sx = "String_x"; 
    const char* sy = "String_y"; 
    const char* sz = "String_z"; 
    max(sx, sy, sz); 
} 
+0

同じシグネチャを有していません。 – Belloc

答えて

4

Why the overload resolution for the call max(x, y) in the expression return max(max(x, y), z); below results in a call to the non-template function char const* max(char const*, char const*) ?

この関数を呼び出すとき:

template <typename T> 
T const& max (T const& x, T const& y, T const& z) 
{ 
    return max (max(x, y), z); 
} 

Tconst char*なると推定されます。したがって、この署名がインスタンス化される:

const char* const& max (
    const char* const& x, 
    const char* const& y, 
    const char* const& z 
    ) 

機能が内部タイプconst char*の引数を持つmax()のバイナリバージョンを呼び出します。タイプテンプレートconst char*の引数に対して、テンプレートとテンプレート以外のオーバーロードが実行可能です。 2つの関数はコールそれらの 1を解決するための実行可能であるしかし

は、非テンプレートバージョンはベストフィットを考えられているテンプレート、ではありません。

C++ 11標準のパラグラフ毎の

13.3.3/1:

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 context is an initialization by user-defined conversion (see 8.5, 13.3.1.5, and 13.3.1.6) and the standard conversion sequence from the return type of F1 to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the return type of F2 to the destination type. [ ... ] or, if not that,

F1 is a non-template function and F2 is a function template specialization, or, if not that,

— F1 and F2 are function template specializations, and the function template for F1 is more specialized than the template for F2 according to the partial ordering rules described in 14.5.6.2.

非テンプレート過負荷が選ばれる理由を説明しています。

+0

テンプレート関数の変換はありません – Belloc

+0

@ user1042389:テンプレート以外の関数でもありません –

+0

'この関数は内部的に、const char型の引数を持つmax()のバイナリバージョンを呼び出します。'これには同意できません。関数 'max()'は、 'const char * const&'型の引数で呼び出されます。 – Belloc

0

Argument Matching - オーバーロードされた関数は、現在のスコープ内の関数宣言に最もよく一致するように選択されています。この例では二つの機能@BoPersson

If template argument deduction succeeds, then the generated function is compared with the other functions to determine the best match, following the rules for overload resolution

 

  • An exact match was found.

  • A trivial conversion was performed.

  • An integral promotion was performed.

  • A standard conversion to the desired argument type exists.

  • A user-defined conversion (either conversion operator or constructor) to the desired argument type exists.

  • Arguments represented by an ellipsis were found.

+0

テンプレート関数の場合、変換はありません。だから私はそれが非テンプレートの代わりに呼び出されるべきだと思います。 – Belloc

関連する問題