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 ?
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.
#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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With