diff --git a/strings/base_string.h b/strings/base_string.h index 6b1fb37b5..b5642e4e3 100644 --- a/strings/base_string.h +++ b/strings/base_string.h @@ -719,8 +719,18 @@ WINRT_EXPORT namespace winrt return{}; } +#if defined(__cpp_lib_string_resize_and_overwrite) && __cpp_lib_string_resize_and_overwrite >= 202110L + std::string result; + result.resize_and_overwrite(size, [&](char* buffer, std::size_t) -> std::size_t + { + auto bytes_written = WINRT_IMPL_WideCharToMultiByte(65001 /*CP_UTF8*/, 0, value.data(), static_cast(value.size()), buffer, size, nullptr, nullptr); + WINRT_VERIFY_(size, bytes_written); + return bytes_written == size ? size : 0; + }); +#else std::string result(size, '?'); WINRT_VERIFY_(size, WINRT_IMPL_WideCharToMultiByte(65001 /*CP_UTF8*/, 0, value.data(), static_cast(value.size()), result.data(), size, nullptr, nullptr)); +#endif return result; } }