The following does not compile:
#include <iostream>
using namespace std;
int x = 5;
int && f () { return std::move(x); }
int g(int & y) { return y; }
int main() {
g(f());
return 0;
}
It's clear to me why non-const lvalue references do not bind to prvalues (unnamed temporaries) -- it does not make sense to modify them, as they will soon disappear. Yet why do non-const lvalue references not bind to xvalues?
If a function returns int &&
, the referenced object can't be temporary, otherwise we would get a dangling reference. Hence if an int &&
is returned, that's, in my understanding, a reference with the additional guarantee that it's safe to move from it.
Aucun commentaire:
Enregistrer un commentaire