Skip to content

Add _bstr user-defined literal for BSTR-shaped wide strings#647

Merged
jonwis merged 11 commits into
microsoft:masterfrom
jonwis:jonwis/static-bstrs-2
Jul 17, 2026
Merged

Add _bstr user-defined literal for BSTR-shaped wide strings#647
jonwis merged 11 commits into
microsoft:masterfrom
jonwis:jonwis/static-bstrs-2

Conversation

@jonwis

@jonwis jonwis commented Jun 27, 2026

Copy link
Copy Markdown
Member

Reduce global initializers and intermediate allocations with a BSTR-compatible string literal.

// Unnecessary globals - 
const wil::unique_bstr g_bst(SysAllocString(L"kittens"));
const auto g_bst2 { wil::make_bstr_failfast(L"puppies"); };

HRESULT UseBstr(BSTR bst);
void call_bstr_function() {
    UseBstr(g_bst.get());

    // Unnecessary local allocation
    auto propName = wil::make_bstr_failfast(L"waffleCount");
    UseBstr(propName.get());

    // !!super mega ultra danger!! - not passing a real bstr...
    UseBstr(L"kablooie");
}

Instead, literals:

const auto g_bst = L"kittens"_bstr;
const auto g_bst2 = L"puppies"_bstr;

void call_bstr_function() {
    UseBstr(g_bst);
    UseBstr(L"waffleCount"_bstr);
}

(Thanks Copilot!)

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>
Comment thread include/wil/stl.h Outdated
Comment thread include/wil/stl.h Outdated
Comment thread tests/StlTests.cpp Outdated
Comment thread include/wil/stl.h Outdated
Comment thread include/wil/stl.h Outdated
Comment thread include/wil/stl.h Outdated
Comment thread include/wil/stl.h
Comment thread include/wil/stl.h Outdated
Jon Wiswall and others added 2 commits July 14, 2026 20:01
Document the two environment gaps that prevent 'scripts\format-changes.cmd'
from working: the LLVM bin directory must be on PATH (VS's clang-format does
not ship git-clang-format), and a 'python3' executable must be resolvable
(disable the Microsoft Store python3 alias on Windows).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: f00e394c-d4c3-47db-a66b-b516ed304a93
@jonwis
jonwis requested a review from dunhor July 17, 2026 15:39
@jonwis jonwis changed the title Add _bst user-defined literal for BSTR-shaped wide strings Add _bstr user-defined literal for BSTR-shaped wide strings Jul 17, 2026
@jonwis
jonwis merged commit 52f17f9 into microsoft:master Jul 17, 2026
15 checks passed
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.

3 participants