Search code examples
c++perfect-forwardingcopy-assignment

Why doesn't perfect forwarding (catch-all) work to implement a copy-assignment?


In a class with a series of ctors (most of which have exactly one argument), I want all one-argument ctors to also be mirrored by a corresponding assignment operator. The ctors include, but are not limited to a copy-ctor and a move-ctor. So this, should satisfy the rule-of-five.

  template <typename T>
  object& operator=(T&& from) {
    // ...
    return *this;
  }

Here is a minimal example: https://ideone.com/OKprcr (thanks to @Daniel H for pointing out constness).

The error I get is

error: object of type 'object' cannot be assigned because its copy assignment operator is implicitly deleted
...
note: copy assignment operator is implicitly deleted because 'object' has a user-declared move constructor

Why doesn't the function template implement the copy-assignment operator?


Solution

  • Why doesn't the function template implement the copy-assignment operator?

    Because the standard says so ([class.copy.assign]/1):

    A user-declared copy assignment operator X::operator= is a non-static non-template member function of class X with exactly one parameter of type X, X&, const X&, volatile X& or const volatile X&.

    Note there's no X&& in there either.