From the C++/WinRT docs
IASyncAction DoWorkAsync(Param const& value) {
auto safe_value = value;
// It's ok to access both safe_value and value here.
co_await DoOtherWorkAsync();
// It's ok to access only safe_value here (not value).
}
This make sense, just a dangling reference. However, in the past I've found I've had to.
IASyncAction DoWorkAsync(Param value) { auto safe_value = value; // It's ok to access both value here. co_await DoOtherWorkAsync(); // It's ok to access only safe_value here (not value). }
and just now I've spent too much time debugging to find out it was this problem yet again.
IASyncAction DoWorkAsync(Param&& value) {
co_await DoOtherWorkAsync(); // value deconstructor called
some_function(std::move(value)); // BOOM
}
Is this suppose to happen? The docs don't mention it. What's going on?
Rvalue references are references; they don't have any special power governing lifetimes outside of the fact that they can manifest temporaries from prvalues like a const&
.
As for value parameters, that depends entirely on what the object is doing. Coroutines already store a copy of the parameter internally. You copying it again at the start of the coroutine is meaningful only to the extent that this copy preserves some resource that the original copy would not. That depends on the type of the parameter.