C++ overloading conversion operator for custom type to std::string

前端 未结 5 731
無奈伤痛
無奈伤痛 2020-12-30 03:52

I hope someone might be able to answer why the following doesn\'t work. Bear with me though, I am still very much a noob... I just cannot get to the bottom of why the follow

5条回答
  •  萌比男神i
    2020-12-30 04:43

    $13.3.1.5/2 states- "The conversion functions of S and its base classes are considered. Those that are not hidden within S and yield type T or a type that can be converted to type T via a standard conversion sequence (13.3.3.1.1) are candidate functions. Conversion functions that return a cv-qualified type are considered to yield the cv-unqualified version of that type for this process of selecting candidate functions. Conversion functions that return “reference to cv2 X” return lvalues of type “cv2 X” and are therefore considered to yield X for this process of selecting candidate functions."

    The assignment s = t works as follows:

    a) All members in the type of 't' (testClass) are considered which can convert 't' to 's'.

    Candidate 1: operator string();   // s created using member string::operator=(string const&)
    Candidate 2: operator char *()    // s created using member string::operator=(char const*)
    Candidate 3: operator char *()    // s created using member string::operator=(char *)
    

    b) All of the above candidates are viable (that is, in absence of other candidates, the compiler can successfully resolve the function call to either of them)

    c) However, now the best viable candidate has to be determined. The Conversion sequences involved are:

    Candidate 1: U1 : operator string()
    Candidate 2: U2 : operator char*()->const qualification to match string::operator=(char const*)
    Candidate 3: U3 : operator char*()
    

    $13.3.3.1.1/3 states - "The rank of a conversion sequence is determined by considering the rank of each conversion in the sequence and the rank of any reference binding (13.3.3.1.4). If any of those has Conversion rank, the sequence has Conversion rank;"

    This means that U1, U2 and U3 are all having Conversion rank and at a first level neither s better than the other. However, the standard also states

    User-defined conversion sequence U1 is a better conversion sequence than another user-defined conversion sequence U2 if they contain the same user-defined conversion function or constructor and if the second standard conversion sequence of U1 is better than the second standard conversion sequence of U2.

    So, let's see what this means.

    Between U1 and U2, they involve, different conversion functions and hence none is better than the other

    Between U1 and U3, they involve, different conversion functions and hence none is better than the other

    So what about U1 and U2. They involve the same conversion function, which satisfies the first part of the "and" condition above

    So what about the part "and if the second standard conversion sequence of U1 is better than the second standard conversion sequence of U2."

    In U2, the second standard conversion sequence requires a const qualification, where in U3 this is not required. The second standard conversion sequence of U3 is an Exact Match.

    But as Table 9 in the Standard states, CV qualification is also considered to be an Exact Match.

    Therefore U2 and U3 are also really indistinguisable as far as overload resolution is considered.

    This means U1, U2 and U3 are all really as good as each other and the compiler finds resolving the call(as part of assignment statement) as ambiguous, as there is no unambiguous best viable function

提交回复
热议问题