Skip to content

Fix set_value template deduction for smart string types#633

Draft
benhillis wants to merge 2 commits into
microsoft:masterfrom
benhillis:fix-set-value-type-const-deduction
Draft

Fix set_value template deduction for smart string types#633
benhillis wants to merge 2 commits into
microsoft:masterfrom
benhillis:fix-set-value-type-const-deduction

Conversation

@benhillis

Copy link
Copy Markdown
Member

set_value_type<> specializations for std::wstring, BSTR, unique_bstr, shared_bstr, unique_cotaskmem_string, and shared_cotaskmem_string incorrectly used const-qualified template parameters (e.g. set_value_type<const std::wstring>).

Since reg_view_t::set_value deduces R from const R& value, the template parameter R is always the non-const type. This caused a static_assert failure (Unsupported type for set_value_type) when calling set_value with any of these types:

\\cpp
std::wstring value{L"Hello"};
wil::reg::set_value(hkey, L"MyValue", value); // static_assert failure
\\

The fix removes the const qualifier from all six affected specializations so the deduced type matches.

Tests added for std::wstring lvalues (non-const, const, and empty) with both opened-key and string-key overloads.

Fixes #624

Comment thread include/wil/registry_helpers.h
The set_value_type<> specializations for std::wstring, BSTR,
unique_bstr, shared_bstr, unique_cotaskmem_string, and
shared_cotaskmem_string incorrectly used const-qualified template
parameters (e.g. set_value_type<const std::wstring>).

Since reg_view_t::set_value deduces R from 'const R& value', the
template parameter R is always the non-const type. This caused a
static_assert failure when calling set_value with any of these types.

Remove the const qualifier from all six affected specializations so
the deduced type matches the specialization.

Fixes microsoft#624
@benhillis benhillis force-pushed the fix-set-value-type-const-deduction branch from 851030a to b2034ad Compare June 10, 2026 16:20
Per review feedback, convert the set_value_type function family into a
set_value_type_t<T> trait struct with a constexpr DWORD value member, and
make set_value_type<T>() a thin wrapper that applies wistd::remove_cv_t.
This removes the need for callers to strip cv-qualifiers from the deduced
type while keeping the type fully specializable for future use.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dunhor

dunhor commented Jun 12, 2026

Copy link
Copy Markdown
Member

Feel free to publish as "ready for review" if this change is ready

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.

wil::reg::set_value doesn't work with std::wstring values

2 participants