Skip to content

Add make_bstr*(std::wstring_view)#634

Open
DrusTheAxe wants to merge 1 commit into
microsoft:masterfrom
DrusTheAxe:user/drustheaxe/unique_bstr-std_wstring_view
Open

Add make_bstr*(std::wstring_view)#634
DrusTheAxe wants to merge 1 commit into
microsoft:masterfrom
DrusTheAxe:user/drustheaxe/unique_bstr-std_wstring_view

Conversation

@DrusTheAxe
Copy link
Copy Markdown
Member

WIL only provides only PCWSTR overloads:

  • make_bstr_nothrow(PCWSTR source)
  • make_bstr_failfast(PCWSTR source)
  • inline wil::unique_bstr make_bstr(PCWSTR source);

This PR adds wstring_view overloads calling SysAllocStringLen with the view's data and size directly, correctly handling non-null-terminated string views

  • make_bstr_nothrow(std::wstring_view source)
  • make_bstr_failfast(std::wstring_view source)
  • make_bstr(std::wstring_view source)

This works for non-null-terminated views (substrings, slices), avoids unnecessary and dangerous wcslen walk (we know the wstring_view's length), and is a strict superset of the existing API.

NOTE: A PCWSTR implicitly converts to wstring_view, so these new overloads could replace the existing ones once C++17 is the floor. WIL still supports older standards, so these additional overloads is safer.

Comment thread include/wil/stl.h
#if defined(__WIL_OLEAUTO_H_)
#if defined(_STRING_VIEW_) || defined(__cpp_lib_string_view)
// Create wil::unique_bstr from std::wstring_view (regardless if not null terminated, or if it contains embedded nulls)
inline wil::unique_bstr make_bstr_nothrow(std::wstring_view source) noexcept
Copy link
Copy Markdown
Collaborator

@dmachaj dmachaj May 9, 2026

Choose a reason for hiding this comment

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

For bonus points - there is also the wil::zwstring_view overloads that enforce null termination but are ortherwise just a string_view.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can basic_zstring_view decay to basic_string_view? Or is there something special preventing object slicing?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It might. I didn't actually check and just assumed a specialization would be needed.

Comment thread include/wil/stl.h
}
inline wil::unique_bstr make_bstr_failfast(std::wstring_view source) noexcept
{
return wil::unique_bstr(FAIL_FAST_IF_NULL_ALLOC(::SysAllocStringLen(source.data(), static_cast<UINT>(source.size()))));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Consider implementing this in terms of the nothrow variant, rather than repeating it here.

Comment thread include/wil/stl.h
}

#if defined(__WIL_OLEAUTO_H_)
#if defined(_STRING_VIEW_) || defined(__cpp_lib_string_view)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the general case, this is a problematic check because <version> also defines __cpp_lib_string_view even though <string_view> may not have been included. That said, this is the stl.h header, so it already (1) assumes STL use and (2) already includes <string_view> with the correct guards, so this concern is moot, but it does make the _STRING_VIEW_ check unnecessary. Better yet if you compare to a specific release. E.g. this could ideally become:

Suggested change
#if defined(_STRING_VIEW_) || defined(__cpp_lib_string_view)
#if __cpp_lib_string_view >= 201606L

... which means you can just move this definition a few lines lower since this check is already present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants