unbox_value_or: Replace param::hstring with hstring - #1558
unbox_value_or: Replace param::hstring with hstring#1558justanotheranonymoususer wants to merge 7 commits into
Conversation
|
This is a performance regression, the following code which would previously use a fast-pass string (and be allocation-free) will now allocate a string on the heap: auto foo = unbox_value_or<hstring>(nullptr, L"bar"); |
|
I don't think this is actually a regression. Let's compare the old and new code for Old code (
New code (
Both paths heap-allocate when the default is used. The function returns The new code is actually better on the success path: zero cost vs. a |
hstring has a move constructor that avoids calling WindowsDuplicateString, which would be used here, no? |
|
If it were a named local, implicit move would kick in: So the old code does copy-construct through Let me know if I'm missing something. Also intuitively, I can't see how |
|
Hmmm yeah you're right, can't move out of a lvalue ref. |
|
@DefaultRyan I believe it's ready. Anything else I should do? |
|
Ping, what do I need to do to have it reviewed and merged? |
There was a problem hiding this comment.
Pull request overview
This PR updates the winrt::unbox_value_or overload for winrt::hstring so the fallback/default value is taken as a type constructible into hstring (instead of param::hstring), avoiding aborts when callers pass non-null-terminated std::wstring_view fallbacks (related to #1527).
Changes:
- Generalize
unbox_value_or’shstringfallback parameter fromparam::hstring const&to a forwarding reference constrained bystd::is_constructible_v<hstring, D>. - Change the fallback return path to construct an owning
hstringfrom the provided default value. - Add tests covering
unbox_value_or<hstring>with multiple string fallback types, including a non-null-terminatedstd::wstring_view.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/test/box_string.cpp | Adds regression/behavior tests for unbox_value_or<hstring> across fallback string input types, including non-null-terminated wstring_view. |
| strings/base_reference_produce.h | Updates unbox_value_or’s hstring overload to accept any D constructible into hstring and construct the fallback safely. |
|
@StephanTLavavej do you have tips on getting it merged? Your #1543 contribution helped, at least it's not closed, but it's a small follow-up change (after #1530) I can't get to get merged :( |
Partially fixes: #1527
Similar to #1530
@DefaultRyan please review