From c0328048e534af36dcf7ed5ab0f4ecb935115eab Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Thu, 8 Jan 2026 12:00:40 +0200 Subject: [PATCH 1/4] box_value constructor: Replace param::hstring with hstring --- strings/base_reference_produce.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strings/base_reference_produce.h b/strings/base_reference_produce.h index 8ea2de5b9..d7ce558c8 100644 --- a/strings/base_reference_produce.h +++ b/strings/base_reference_produce.h @@ -509,12 +509,12 @@ namespace winrt::impl WINRT_EXPORT namespace winrt { - inline Windows::Foundation::IInspectable box_value(param::hstring const& value) + inline Windows::Foundation::IInspectable box_value(hstring value) { - return Windows::Foundation::IReference(*(hstring*)(&value)); + return Windows::Foundation::IReference(std::move(value)); } - template , int> = 0> + template , int> = 0> Windows::Foundation::IInspectable box_value(T const& value) { if constexpr (std::is_base_of_v) From 5ea3fd5adb7851f450bfb670c8b1141842160416 Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Sat, 21 Mar 2026 14:31:04 +0200 Subject: [PATCH 2/4] Update base_reference_produce.h --- strings/base_reference_produce.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/strings/base_reference_produce.h b/strings/base_reference_produce.h index 5c32c4a59..7c1be57ac 100644 --- a/strings/base_reference_produce.h +++ b/strings/base_reference_produce.h @@ -509,9 +509,10 @@ namespace winrt::impl WINRT_EXPORT namespace winrt { - inline Windows::Foundation::IInspectable box_value(hstring value) + template , int> = 0> + Windows::Foundation::IInspectable box_value(T&& value) { - return Windows::Foundation::IReference(std::move(value)); + return Windows::Foundation::IReference(hstring(std::forward(value))); } template , int> = 0> From 2b9bf6fdeae5ad93ee6180c2d639a5fe2f74f761 Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Mon, 23 Mar 2026 23:12:22 +0200 Subject: [PATCH 3/4] Fix and add tests --- strings/base_reference_produce.h | 18 +++++------ test/test/box_string.cpp | 53 ++++++++++++++++++++++++++++++++ test/test/test.vcxproj | 1 + 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 test/test/box_string.cpp diff --git a/strings/base_reference_produce.h b/strings/base_reference_produce.h index 7c1be57ac..bb399c95c 100644 --- a/strings/base_reference_produce.h +++ b/strings/base_reference_produce.h @@ -23,37 +23,37 @@ namespace winrt::impl return std::is_arithmetic_v || std::is_enum_v; } - std::uint8_t GetUInt8() const +std::uint8_t GetUInt8() const { return to_scalar(); } - std::int16_t GetInt16() const +std::int16_t GetInt16() const { return to_scalar(); } - std::uint16_t GetUInt16() const +std::uint16_t GetUInt16() const { return to_scalar(); } - std::int32_t GetInt32() const +std::int32_t GetInt32() const { return to_scalar(); } - std::uint32_t GetUInt32() const +std::uint32_t GetUInt32() const { return to_scalar(); } - std::int64_t GetInt64() const +std::int64_t GetInt64() const { return to_scalar(); } - std::uint64_t GetUInt64() const +std::uint64_t GetUInt64() const { return to_scalar(); } @@ -509,13 +509,13 @@ namespace winrt::impl WINRT_EXPORT namespace winrt { - template , int> = 0> + template , int> = 0> Windows::Foundation::IInspectable box_value(T&& value) { return Windows::Foundation::IReference(hstring(std::forward(value))); } - template , int> = 0> + template , int> = 0> Windows::Foundation::IInspectable box_value(T const& value) { if constexpr (std::is_base_of_v) diff --git a/test/test/box_string.cpp b/test/test/box_string.cpp new file mode 100644 index 000000000..efff92160 --- /dev/null +++ b/test/test/box_string.cpp @@ -0,0 +1,53 @@ +#include "pch.h" + +TEST_CASE("box_string") +{ + // hstring + { + winrt::hstring value = L"hstring"; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"hstring"); + } + + // wchar_t const* (string literal) + { + auto boxed = winrt::box_value(L"literal"); + REQUIRE(winrt::unbox_value(boxed) == L"literal"); + } + + // std::wstring + { + std::wstring value = L"wstring"; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"wstring"); + } + + // std::wstring_view (null-terminated) + { + std::wstring_view value = L"view"; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"view"); + } + + // std::wstring_view (not null-terminated) + // Regression test for https://github.com/microsoft/cppwinrt/issues/1527 + { + std::wstring source = L"ABCDE"; + std::wstring_view value(source.data(), 3); // "ABC" + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L"ABC"); + } + + // Empty string + { + auto boxed = winrt::box_value(winrt::hstring{}); + REQUIRE(winrt::unbox_value(boxed) == L""); + } + + // Empty wstring_view + { + std::wstring_view value; + auto boxed = winrt::box_value(value); + REQUIRE(winrt::unbox_value(boxed) == L""); + } +} diff --git a/test/test/test.vcxproj b/test/test/test.vcxproj index 7840f17eb..43928dab2 100644 --- a/test/test/test.vcxproj +++ b/test/test/test.vcxproj @@ -233,6 +233,7 @@ + NotUsing From 975eaa0e09f1272ed0451bd42b1c3794687b80ba Mon Sep 17 00:00:00 2001 From: justanotheranonymoususer Date: Mon, 23 Mar 2026 23:13:59 +0200 Subject: [PATCH 4/4] Fix github.dev bug --- strings/base_reference_produce.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/strings/base_reference_produce.h b/strings/base_reference_produce.h index bb399c95c..2820aff50 100644 --- a/strings/base_reference_produce.h +++ b/strings/base_reference_produce.h @@ -23,37 +23,37 @@ namespace winrt::impl return std::is_arithmetic_v || std::is_enum_v; } -std::uint8_t GetUInt8() const + std::uint8_t GetUInt8() const { return to_scalar(); } -std::int16_t GetInt16() const + std::int16_t GetInt16() const { return to_scalar(); } -std::uint16_t GetUInt16() const + std::uint16_t GetUInt16() const { return to_scalar(); } -std::int32_t GetInt32() const + std::int32_t GetInt32() const { return to_scalar(); } -std::uint32_t GetUInt32() const + std::uint32_t GetUInt32() const { return to_scalar(); } -std::int64_t GetInt64() const + std::int64_t GetInt64() const { return to_scalar(); } -std::uint64_t GetUInt64() const + std::uint64_t GetUInt64() const { return to_scalar(); }