2

I can't understand :Why if a member function has a reference qualifier, all versions with the same list must have a reference qualifier. Is it because multiple definitions are created when this function is called?

class A{
   public:
         A sorter() &&;
         A sorter();
         A operator+ (const A&);
       }

Although there is only function declaration above, it should be enough

A a , b;
(a+b).sorter();

Does the definition and calling of a function like the above make the compiler unable to recognize which member function to use?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Nater
  • 165
  • 7

1 Answers1

3

When one overload is reference-qualified, the others must be as well. This works:

     A sorter() &&;
     A sorter() &;

Put another way: you can choose between two sets of possible overloads:

  1. const and non-const (unqualified).
  2. const& (synonymous with const in 1), & (non-const), and && (rvalue/temporary).
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • That's what I'm asking about,What's the meaning of doing this? To eliminate syntax errors, if only one member function is defined with a reference, will double definitions be created? Or just the syntax of C + + – Nater Apr 25 '20 at 07:27
  • 1
    @Nater: This is because a unqualified member function corresponds to both `&` and `&&`, i.e. it would be ambiguous. For more on this: https://stackoverflow.com/questions/35389911/why-is-overloading-on-just-one-ref-qualifier-not-allowed – John Zwinck Apr 25 '20 at 08:04