[clang++] Constructor "template<class U> T(U&&)" is better that copy constructor "T(const T&)"
Example
#include <iostream>
class CLangTester {
public:
CLangTester()=default;
CLangTester(const CLangTester&){
std::cout << "Copy\n";
};
CLangTester(CLangTester&&){
std::cout << "Move\n";
};
template<class U>
CLangTester(U&&){
std::cout << "Other\n";
};
};
int main(){
CLangTester c1;
CLangTester c2(c1);
}
returns "Other". https://godbolt.org/z/5dP5vx8o3
But: [over.ics.ref/1.2]
When a parameter of type “reference to cv T” binds directly ([dcl.init.ref]) to an argument expression: [...] -- Otherwise, if the type of the argument is possibly cv-qualified T, or if T is an array type of unknown bound with element type U and the argument has an array type of known bound whose element type is possibly cv-qualified U, the implicit conversion sequence is the identity conversion. [...]
and [over.best.ics/general-example-2]
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.
and [over.ics.scs/2]
a standard conversion sequence either is the Identity conversion by itself (that is, no conversion)
and [over.ics.scs#tab:over.ics.scs-row-2]
No conversions required
and this determines that template<class U> CLangTester(U&&) is not better than CLangTester(const CLangTester&)?
Is the expected result "ambiguous overload resolution" or is CLangTester(const CLangTester&) better?
@llvm/issue-subscribers-c-1
Author: None (safocl)
class CLangTester { public: CLangTester()=default; CLangTester(const CLangTester&){ std::cout << "Copy\n"; };
CLangTester(CLangTester&&){
std::cout << "Move\n";
};
template<class U>
CLangTester(U&&){
std::cout << "Other\n";
};
};
int main(){ CLangTester c1; CLangTester c2(c1); }
returns "Other".
https://godbolt.org/z/5dP5vx8o3
But:
[[over.ics.ref/1.2](https://eel.is/c++draft/over.ics.ref#<!-- -->1.2)]
> When a parameter of type “reference to cv T” binds directly ([dcl.init.ref]) to an argument expression:
> [...]
> -- Otherwise, if the type of the argument is possibly cv-qualified T, or if T is an array type of unknown bound with element type U and the argument has an array type of known bound whose element type is possibly cv-qualified U, the implicit conversion sequence is the identity conversion.
> [...]
and [[over.best.ics/general-example-2](https://eel.is/c++draft/over.best.ics#general-example-2)]
> A parameter of type A can be initialized from an argument of type const A[.](https://eel.is/c++draft/over.best.ics#general-6.sentence-5) The implicit conversion sequence for that case is the identity sequence; it contains no “conversion” from const A to A[.](https://eel.is/c++draft/over.best.ics#general-6.sentence-6)
and [[over.ics.scs/2](https://eel.is/c++draft/over.ics.scs#<!-- -->2)]
> a standard conversion sequence either is the Identity conversion by itself (that is, no conversion)
and [[over.ics.scs#tab:over.ics.scs-row-2](https://eel.is/c++draft/over.ics.scs#tab:over.ics.scs-row-2)]
> No conversions required
and this determines that `template<class U> CLangTester(U&&)` is not better than `CLangTester(const CLangTester&)`?
Is the **expected** result "ambiguous overload resolution" or is CLangTester(const CLangTester&) better?
</details>
U is deduced to be CLangTester& and it wins a "tie breaker" [over.ics.rank]/3.2.6 against const CLangTester&.
Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if [...] -- S1 and S2 bind “reference to T1” and “reference to T2”, respectively ([dcl.init.ref]), where T1 and T2 are not the same type, and T2 is reference-compatible with T1