#include <fmt/core.h>
struct S {
std::string getValue() && {
return std::move(value);
}
const std::string& getValue() const& {
return value;
}
private:
std::string value;
};
S foo() { return {}; }
int main() {
S s;
fmt::print("{}", s.getValue(), foo().getValue());
}
当左值调用时,const &
被调用,返回指向对象内部的引用;当右值调用时,&&
被调用,内部的值被move,而不是返回引用,因为内部对象随后可能不存在,此处RVO不起作用,只能move