Sometimes you’ll encounter situations where a variable is still in scope, but cannot legally be accessed anymore. A typical example is if the variable has been moved from and we want to make sure we don’t accidentally reference it again, but there are other examples as well, for example during asynchronous execution.
This is a clever way to explicitly hide names: How can I prevent myself from accessing a lambda captured variable or a parameter after I’m done with it?
Example taken from the post, with extra comments added to explain the WinRT resume_background()
function and consequences of its use:
using hide_name = void(struct hidden_name);
// This used to refresh synchronously, but now we refresh
// asynchronously. The parameter is still passed by reference
// for compatibility.
winrt::fire_and_forget Refresh(winrt::com_ptr<Widget> const& widget) {
// note: execution begins on the UI thread
auto copiedWidget = widget;
hide_name widget; // hide the original reference!
// note: after this, the remainder of the function executes on another
// thread, meaning that operating on 'widget' could cause a data race
co_await winrt::resume_background();
copiedWidget->Reset();
copiedWidget->Reload();
}