If they weren't, that would be very problematic.
Indeed, consider the following code :
int function(){
MyClass myObject;
//stuff
return 5 + myObject.getNumericalValue();
}
with getNumericalValue()
a simple member function that returns an int based on computations on member variable. If the expression was evaluated after the destruction of myObject
, you would have undefined behavior, and using locals would be impossible in return statement (which isn't the case).
In your case, the lock will be destroyed after the evaluation of the return statement.
To add some strictness to that, let me quote the standard (§3.7.3/3, emphasis mine) :
If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be eliminated as an optimization even if it appears to be unused
The end of the block, for a function, is after the return statement.