Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Ref-qualified member functions ? [duplicate]

Tags:

While reading http://en.cppreference.com/w/cpp/language/member_functions, I came across something, I haven't seen before: lvalue/rvalue Ref-qualified member functions. What would be their purpose ?

like image 564
newprint Avatar asked Oct 20 '13 05:10

newprint


1 Answers

Just read down below:

During overload resolution, non-static cv-qualified member function of class X is treated as a function that takes an implicit parameter of type lvalue reference to cv-qualified X if it has no ref-qualifiers or if it has the lvalue ref-qualifier. Otherwise (if it has rvalue ref-qualifier), it is treated as a function taking an implicit parameter of type rvalue reference to cv-qualified X.

Example

#include <iostream> struct S {     void f() & { std::cout << "lvalue\n"; }     void f() &&{ std::cout << "rvalue\n"; } };   int main(){     S s;     s.f();            // prints "lvalue"     std::move(s).f(); // prints "rvalue"     S().f();          // prints "rvalue" } 

So during overload resolution the compiler looks for the function &-qualified if the caller object is an lvalue or for the function &&-qualified if the caller object is an rvalue.

like image 123
FKaria Avatar answered Sep 16 '22 12:09

FKaria