0

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?

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • 2
    Hard to tell without a [MCVE]. Does `object` have any non-copyable data member? – Vittorio Romeo Nov 13 '17 at 20:57
  • @VittorioRomeo Yes, it has an `std::unique_ptr` pimpl, but that's why I am implementing a custom copy-assignment. Maybe that's why I couldn't create a minimal example. I'll check. – bitmask Nov 13 '17 at 20:59
  • Nope, that wasn't it. https://ideone.com/KM4NMD – bitmask Nov 13 '17 at 21:00
  • @bitmask A minimal example doesn’t mean an example that *compiles*, it means an example which *shows the same issue*. In your case a minimal verifiable example would be code that we could plug into a compiler to get the same error message. – Daniel H Nov 13 '17 at 21:00
  • I realise that. The problem is, whenever I rip the problem out, it suddenly compiles fine. – bitmask Nov 13 '17 at 21:01
  • @bitmask Of course it compiles fine when you rip out the part which doesn’t compile. You should include a minimal example which *shows this compiler error* in the question. – Daniel H Nov 13 '17 at 21:03
  • @DanielH Look, I'm taking the part that should reproduce the problem (see ideone) but outside my project it does not trigger the error, (no idea why) while I get a problem inside my project for missing a copy-assignment. – bitmask Nov 13 '17 at 21:05
  • @bitmask Oh, I misread what you were saying. – Daniel H Nov 13 '17 at 21:17
  • 1
    My *guess* is that it’s `const`-related. If I make `p` into a `const object` in your example, [it fails](https://godbolt.org/g/sRdth2). Try making the assignment operator take in a `T const&&`. Also, you should check for self-assignment and self-move, as your code gives UB in those cases. – Daniel H Nov 13 '17 at 21:20
  • Ah yes, good point. However, adding `const` to the argument (either `T const&` or `T const&&`) doesn't appear to change anything. – bitmask Nov 13 '17 at 21:33
  • Added a MCVe @VittorioRomeo. – bitmask Nov 13 '17 at 21:39

1 Answers1

2

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243