|
|
|
|
|
by bodyfour
3997 days ago
|
|
I think concentrating on the move part is the wrong way to look at rvalue-ref methods. The advantage is that if the compiler knows an object is a temporary it you can safely let the caller safely "steal" a member object. #include <string>
#include <stdio.h>
struct A {
const std::string& str() const & { return m_str; }
std::string&& str() && { return std::move(m_str); }
std::string m_str;
};
void func(const std::string& s) { puts("Got const value"); }
void func(std::string&& s) { puts("Got rvalue"); }
A returns_a() { A a; return a; }
int main()
{
func(returns_a().str());
A b;
func(b.str());
return 0;
}
|
|