Allow delegates to be created with weak reference + lambda#1372
Merged
jonwis merged 2 commits intomicrosoft:mastermicrosoft/cppwinrt:masterfrom Nov 26, 2023
Merged
Allow delegates to be created with weak reference + lambda#1372jonwis merged 2 commits intomicrosoft:mastermicrosoft/cppwinrt:masterfrom
jonwis merged 2 commits intomicrosoft:mastermicrosoft/cppwinrt:masterfrom
Conversation
We have found that a very common pattern for event handlers is
to capture a weak reference into a lambda, and in the event handler,
try to upgrade the weak reference to a strong one, and if so, do some work:
```cpp
widget.Closed([weak = get_weak(), data](auto&& sender, auto&& args)
{
if (auto strongThis = weak.get())
{
strongThis->do_all_the_things(data);
}
});
```
This commit extends the existing delegate constructors to permit a
`winrt::weak_ref` + lambda (or `std::weak_ptr` + lambda), which
simplifies the above to
```cpp
widget.Closed({ get_weak(), [this, data](auto&& sender, auto&& args)
{
do_all_the_things(data);
} });
```
## Implementation notes
A lambda and pointer to member function are hard to distinguish
in a template parameter list. In theory, we could use SFINAE or
partial specialization, but a simpler solution is to distinguish
the two inside the body of the constructor, via
`std::is_member_function_pointer_v`.
The `com_ptr` and `shared_ptr` variants of the test were
unified, since I found myself editing two nearly identical tests.
Fixes microsoft#1371
kennykerr
approved these changes
Nov 24, 2023
Collaborator
|
Reran the test but same result. Not sure why the test is failing. |
jonwis
approved these changes
Nov 25, 2023
Member
|
Looking at yvals_core.h, I see this: #if __clang_major__ < 16
_EMIT_STL_ERROR(STL1000, "Unexpected compiler version, expected Clang 16.0.0 or newer.");
#endif // ^^^ old Clang ^^^And the build setup output says this: I'm new to GH actions & pipelines, but I see a reference to 15.0.5 in https://github.com/microsoft/cppwinrt/blob/master/.github/actions/setup-llvm-msvc/action.yml ... the latest LLVM hot off of |
Collaborator
|
The github VMs probably got updated. |
learn-build-service-prod bot
pushed a commit
to MicrosoftDocs/winrt-related
that referenced
this pull request
Jul 14, 2025
* New delegate constructors coming soon Also add documentation for what the constructors are. * New delegate constructors New delegate constructors introduced by microsoft/cppwinrt#1372 * Fill in version numbers Also, the `object` parameter could be a smart pointer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We have found that a very common pattern for event handlers is to capture a weak reference into a lambda, and in the event handler, try to upgrade the weak reference to a strong one, and if so, do some work:
This commit extends the existing delegate constructors to permit a
winrt::weak_ref+ lambda (orstd::weak_ptr+ lambda), which simplifies the above towidget.Closed({ get_weak(), [this, data](auto&& sender, auto&& args) { do_all_the_things(data); } });Implementation notes
A lambda and pointer to member function are hard to distinguish in a template parameter list. In theory, we could use SFINAE or partial specialization, but a simpler solution is to distinguish the two inside the body of the constructor, via
std::is_member_function_pointer_v.The
com_ptrandshared_ptrvariants of the test were unified, since I found myself editing two nearly identical tests.Fixes: #1371