0

What does the & after a method signature mean and do? I have this:

Iterator operator++(int) &

in my iterator types but I do not recall why I had to put it there (only that it fixed the problem I had a while back). Strangely I cannot find much about it but it is most likely because I do not know how it is actually called.

A bonus question but is it part of the signature? If yes, how does the pointer to this method look like (where does the & go)?

EDIT: To answer the bonus question: It is part of the signature (and can be overload). Having ref-qualified signature means you cannot have non-ref qualified one though. And the pointer would be: Iterator(Iterator::*ptr)(int) & = &Iterator::operator++;

Resurrection
  • 3,462
  • 2
  • 27
  • 47
  • 1
    It's a reference [qualified](https://en.cppreference.com/w/cpp/language/member_functions#const-.2C_volatile-.2C_and_ref-qualified_member_functions) member function. Basically it means that the function can only be called on objects that can be an lvalue references. – Some programmer dude Jan 26 '20 at 09:23
  • @Someprogrammerdude Yes it does, thank you. Just to clarify as the link to cppreference nor the related question asnwers it - is it part of the function signature? (experimentally tested it seems not) – Resurrection Jan 26 '20 at 09:32
  • `const` and `volatile` qualification for member functions is part of the signature (that is you can overload on `const` and non-`const` qualification). I don't know about reference qualification, but the example shows overloading of lvalue- and rvalue-reference qualification so it must be part of the signature. – Some programmer dude Jan 26 '20 at 09:37
  • 1
    @Someprogrammerdude Ah right, it is a bit strange. You can overload on `&` and `&&` but then you cannot have the version without any ref-qualifiers (because it would be ambiguous and kind of nonsensical as non-ref-qualifed signature can bind to both). And for the curious the pointer would be (for my example in OP): `Iterator(Iterator::*ptr)(int) & = &Iterator::operator++;` – Resurrection Jan 26 '20 at 09:47

1 Answers1

1

This is an lvalue-ref qualifier. It is explained here.

During overload resolution, non-static cv-qualified member function of class X is treated as follows:

  • [...]

  • lvalue ref-qualifier: the implicit object parameter has type lvalue reference to cv-qualified X

This means that the member function can only be used on an lvalue. In simpler but kind of imprecise terms, this prevents calling the function on a temporary.

Community
  • 1
  • 1
patatahooligan
  • 2,642
  • 16
  • 28