Skip to content

Fix set_value template deduction for smart string types#633

Draft
benhillis wants to merge 3 commits intomicrosoft:masterfrom
benhillis:fix-set-value-type-const-deduction
Draft

Fix set_value template deduction for smart string types#633
benhillis wants to merge 3 commits intomicrosoft: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

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
Comment thread include/wil/registry_helpers.h
Replace function template specializations with a set_value_type_t struct
template approach. The set_value_type<T>() wrapper function now applies
wistd::remove_cv_t<T> internally, so callers never need to worry about
cv-qualification of the template parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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