2

My question is in reference to the following links:

Why declare a deleted assignment operators with the ref-qualifier &

Should I use lvalue reference qualifiers for assignment operators?

I could not follow the posts. Could somebody explain:

  1. What does the & at the end in the given code snippet actually do? auto operator=(const string& rhs) &;.
  2. Why does the & at the end of this given code snippet is redundant? (Please also confirm if its just redundant or it is problematic) auto operator=(const string& rhs) & = delete;
  3. Is this statements is valid, auto operator=(const string&& rhs) &&;?

If qualifier & prevents copy assignment of rvalue, Is it not already taken care by the type of input parameter specified? (const string& rhs).

In the following example, with or without qualifier, I get same result for rvalue assignment.

class copy_assignment{
   public: 
   copy_assignment& operator=(copy_assignment& a) & {cout << "copied"; return a;}; 
   copy_assignment* get_ca(){return new(copy_assignment);}
};

int main()
{
    copy_assignment a, b;
    a = *b.get_ca();
}
SNR_BT
  • 133
  • 5
  • The title of the first question basically answers this: it's a ref-qualifier on a non-static member function. You could research that in a C++ book or on sites like cppreference to find out more. – underscore_d Jul 10 '20 at 13:28
  • 3
    The linked posts appear to address the questions. Could you please clarify which part you don't understand? – cigien Jul 10 '20 at 13:28
  • *"I could not follow the posts."* is equivalent to *"My code doesn't work."* in terms of information it (doesn't) convey. We don't know what the problem is, we can't explain how to fix it. – François Andrieux Jul 10 '20 at 13:39

0 Answers0