Add _bst user-defined literal for BSTR-shaped wide strings#647
Conversation
C++20 only. Captures the literal as a class-type NTTP so storage is
sized exactly to the literal length; constexpr-constructible, no heap,
no lifetime hazards.
void Use(BSTR);
Use(L\"foo\"_bst);
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
|
||
| WI_NODISCARD constexpr operator BSTR() const WI_NOEXCEPT | ||
| { | ||
| return const_cast<wchar_t*>(&m_data[0]); |
There was a problem hiding this comment.
Do you need to guard against N=0? In that case this is getting the address of a zero-size buffer.
There was a problem hiding this comment.
Zero sized arrays are not valid in C++
| template <wchar_literal_storage Lit> | ||
| WI_NODISCARD constexpr auto operator""_bst() WI_NOEXCEPT | ||
| { | ||
| return bstr_literal_t<sizeof(Lit.value) / sizeof(wchar_t)>{Lit.value}; |
There was a problem hiding this comment.
Add something like:
static constexpr const std::size_t size = N;or a (constexpr) size() method to wchar_literal_storage so you don't need the manual length calculation
| #if __WI_LIBCPP_STD_VER >= 20 | ||
| SECTION("Literal creates a valid BSTR") | ||
| { | ||
| const auto literal = L"foo"_bst; |
There was a problem hiding this comment.
Should also include a constexpr validation
| struct bstr_literal_t | ||
| { | ||
| unsigned long m_byte_length; | ||
| wchar_t m_data[N]; |
There was a problem hiding this comment.
Consider: something like static_assert(offsetof(m_data) == 4)
| }; | ||
|
|
||
| template <wchar_literal_storage Lit> | ||
| WI_NODISCARD constexpr auto operator""_bst() WI_NOEXCEPT |
There was a problem hiding this comment.
My personal expectation is that this would be _bstr, but I don't use BSTRs enough to know how common of an "abbreviation" this is.
There was a problem hiding this comment.
One thing worth mentioning is that "bst" could stand for "binary search tree" (not that that would make much sense for a user defined literal)
Reduce global initializers and intermediate allocations with a
BSTR-compatible string literal.Instead, literals:
(Thanks Copilot!)