llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang++] Constructor "template<class U> T(U&&)" is better that copy constructor "T(const T&)"

Open safocl opened this issue 1 year ago • 2 comments

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?

safocl avatar Jan 03 '25 17:01 safocl

@llvm/issue-subscribers-c-1

Author: None (safocl)

Example ```cpp #include <iostream>

class CLangTester { public: CLangTester()=default; CLangTester(const CLangTester&){ std::cout << "Copy\n"; };

CLangTester(CLangTester&amp;&amp;){
    std::cout &lt;&lt; "Move\n";
};

template&lt;class U&gt;
CLangTester(U&amp;&amp;){
    std::cout &lt;&lt; "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)]

&gt; When a parameter of type “reference to cv T” binds directly ([dcl.init.ref]) to an argument expression:
&gt; [...]
&gt;     -- 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.
&gt; [...]

and [[over.best.ics/general-example-2](https://eel.is/c++draft/over.best.ics#general-example-2)]

&gt; 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)]
&gt; 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)]
&gt; No conversions required

and this determines that `template&lt;class U&gt; CLangTester(U&amp;&amp;)` is not better than `CLangTester(const CLangTester&amp;)`?
Is the **expected** result "ambiguous overload resolution" or is CLangTester(const CLangTester&amp;) better?
</details>

llvmbot avatar Jan 03 '25 18:01 llvmbot

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

MagentaTreehouse avatar Jan 03 '25 19:01 MagentaTreehouse