In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

后端 未结 1 2084
野趣味
野趣味 2021-02-03 23:49

The question arose while I was researching the answer to this SO question. Consider the following code:

struct A{
    operator char() const{ return \'a\'; }
             


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 23:59

    When deciding the best user-defined conversion for a user-defined conversion sequence we have an overload candidate set.

    §13.3.3/p1 says:

    Define ICSi(F) as follows:

    • [...]

    • let ICSi(F) denote the implicit conversion sequence that converts the i-th argument in the list to the type of the i-th parameter of viable function F. 13.3.3.1 defines the implicit conversion sequences and 13.3.3.2 defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another.

    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

    — [...]

    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.

    This applies since

    §13.3.3.1.2/p2

    2 The second standard conversion sequence converts the result of the user-defined conversion to the target type for the sequence. Since an implicit conversion sequence is an initialization, the special rules for initialization by user-defined conversion apply when selecting the best user-defined conversion for a user-defined conversion sequence (see 13.3.3 and 13.3.3.1).

    and therefore the conversion sequence involving operator int is selected as best match.

    Finally I would rephrase §13.3.3.1/p10 as

    If several different sequences of conversions exist that each convert the argument to the parameter type and it was not possible to determine a best candidate, the implicit conversion sequence associated with the parameter is defined to be the unique conversion sequence designated the ambiguous conversion sequence. For the purpose of ranking implicit conversion sequences as described in 13.3.3.2, the ambiguous conversion sequence is treated as a user-defined sequence that is indistinguishable from any other user-defined conversion sequence134. If a function that uses the ambiguous conversion sequence is selected as the best viable function, the call will be ill-formed because the conversion of one of the arguments in the call is ambiguous.

    0 讨论(0)
提交回复
热议问题