On Tue, 7 May 2024 10:36:25 +0200 Sebastian Wittmeier wrote:>So in your case, you want to have a reference type deduced (by template argument deduction),>but currently [expr.type]/1 disagrees (https://timsong-cpp.github.io/cppwp/expr.type#1): >If an expression initially has the type ?reference to T? ([dcl.ref], [dcl.init.ref]), the type is >adjusted to T prior to any further analysis.
>The copy constructor is something special (compared to other constructors).
>Why do you want to handle it by a template
>in the first place?Yes, I want to have a reference type deduced, but not through the template parameter deduction mechanism PR87332. The template parameter deduction mechanism leads to T, not T&. "template <typename T> base(T x) {}" prevents the compiler from calling the default copy constructor.
struct Base {int b;template<class T> Base(T);// here the compiler also automatically generates Base(const Base&)=default};struct Derived : Base {int d;Derived(const Derived& rhs) : Base(rhs), d(rhs.d) {} // manually calls Base::Base<Derived>(Derived)};Derived copy(const Derived& rhs) { return rhs; } // infinite recursion at runtime
struct Base {int b;template<class T> Base(T);// here the compiler also automatically generates Base(const Base&)=default};struct Derived : Base {int d;Derived(const Derived& rhs) = default; // automatically calls Base::Base(const Base&)};Derived copy(const Derived& rhs) { return rhs; } // OK
struct Base {
int b;template<class T> Base(T);// here the compiler also automatically generates Base(const Base&)=default};struct Derived : Base {int d;Derived(const Derived& rhs) : Base((const Base&)rhs), d(rhs.d) {} // manually calls Base::Base(const Base&)};Derived copy(const Derived& rhs) { return rhs; } // OK