Skip to content

Conversation

egorzhdan
Copy link
Contributor

This adds CxxStdlib overlay features to std::wstring similarly to std::string, std::u16string, etc.

This lets clients e.g. convert between Swift String and C++ wide string simply by calling an initializer.

rdar://159272493

This adds CxxStdlib overlay features to `std::wstring` similarly to `std::string`, `std::u16string`, etc.

This lets clients e.g. convert between Swift String and C++ wide string simply by calling an initializer.

rdar://159272493
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Aug 27, 2025
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

public init(_ string: String) {
self.init()
#if os(Windows)
for char in string.utf16 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would relying on something like string.withCString(encodedAs: UTF16.self) be more efficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite possibly, yes, although this is copied verbatim from the std::u16string constructor, so I think we should update both constructors in a separate PR.

@@ -95,6 +95,30 @@ extension std.u32string {
}
}

extension std.wstring {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make sense auto conforming wstring to the corresponding u*string instead. Aren't they supposed to desugar to the same type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, they are distinct types! wchar_t isn't the same type as char16_t or char32_t, even if the bitwidth matches.

@egorzhdan egorzhdan merged commit 521c2ce into swiftlang:main Aug 28, 2025
5 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/wstring branch August 28, 2025 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants