2

A few questions on SO use a particular syntax for declaring default assignment operators.

Rule-of-Three becomes Rule-of-Five with C++11?

class C {
  C(const C&) = default;
  C(C&&) = default;
  C& operator=(const C&) & = default;
  C& operator=(C&&) & = default;
  virtual ~C() { }
};

I'm confused by the & = used for the assignment operators. After a quick test, default assignment operator declarations seem to compile and give the expected behavior with or without the additional ampersand.

I don't see the & = syntax on cppreference.

Community
  • 1
  • 1
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
  • 1
    It is not `&=`. The `&` forms part of the signature. – juanchopanza Jun 30 '14 at 06:51
  • 1
    See also the following answer for the usage with the assignment operator: http://stackoverflow.com/questions/13099942/should-i-still-return-const-objects-in-c11/13099997#13099997 – nosid Jun 30 '14 at 06:54

1 Answers1

2

The & there is a ref qualifier.

In that particular case it makes it so that the instance of C you want to assign to must be a non-const lvalue.

mpark
  • 7,574
  • 2
  • 16
  • 18