Function with parameter type that has a copy-constructor with non-const ref chosen?

前端 未结 2 341
再見小時候
再見小時候 2021-01-21 13:18

Some time ago I was confused by the following behavior of some code when I wanted to write a is_callable trait. Overload resolution won\'t call fu

2条回答
  •  借酒劲吻你
    2021-01-21 14:21

    Basically, for overload resolution, it is assumed that an object of type A can be converted to an object of type A regardless of any cv-qualification on either of the two.

    From draft n1905:

    13.3.3.1: Overloading.Overload Resolution.Best Viable Function.Implicit Conversion Sequences

    6 When the parameter type is not a reference, the implicit conversion sequence models a copy-initialization of the parameter from the argument expression. The implicit conversion sequence is the one required to convert the argument expression to an rvalue of the type of the parameter. [ Note: when the parameter has a class type, this is a conceptual conversion defined for the purposes of clause 13; the actual initialization is defined in terms of constructors and is not a conversion. — end note ] Any difference in top-level cv-qualification is subsumed by the initialization itself and does not constitute a conversion. [ Example: a parameter of type A can be initialized from an argument of type const A. The implicit conversion sequence for that case is the identity sequence; it contains no “conversion” from const A to A. — end example ] When the parameter has a class type and the argument expression has the same type, the implicit conversion sequence is an identity conversion. [...]

提交回复
热议问题