0

I found the C++ class with API like this:

class foo
{
.....
public:
int& func1() & ;
int func1() && ;
.....
}

what does operator & and && do after the method name and what is the difference between these two function.

Nader
  • 318
  • 1
  • 6
  • 16

1 Answers1

3

These are called "ref qualifiers" and allow you to overload a member function depending on the value category of *this.

  • int& func1() & means: this overload of func1 can be invoked on any instance of *this which can be bound to an lvalue reference.

  • int func1() && means: this overload of func1 can be invoked on any instance of *this which can be bound to an rvalue reference.

E.g.

foo f; f.func1(); // calls &-qualified version
foo{}.func1();    // calls &&-qualified version
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416