In C++, you can't assign a reference to an expression that returns a value.
int & ref = 10 + 10; //Error
I noticed, however, that you can assign a Rvalue to an expression that returns a value:
int && Rref = 10 + 10; //This is ok
Does the lifetime of Rref
extend until the end of the scope, or is using Rref
undefined behavior?
In a slightly more complicated example, let's take a class with a nontrivial constructor and destructor. It manages a pointer to something else.
//These are defined somewhere else
struct State;
State* makeState() noexcept;
void transformState(State*) noexcept;
void cleanState(State*) noexcept;
//Creates a state and cleans up the state at the end
struct Blitz {
State* state;
Blitz() {
state = makeState();
}
~Blitz() {
cleanState(state);
}
};
void dostuff() {
auto&& myBlitz = Blitz();
transformState(myBlitz.state);
}
In the function dostuff
, does the standard guarantee that the destructor won't be called until the end of the function?
Aucun commentaire:
Enregistrer un commentaire