Skip to content

Refuse a mutable proxy_cast on a const contained value - #80

Open
mingxwa wants to merge 1 commit into
ngcpp:feature/v5from
mingxwa:user/mingxwa/proxy-cast-constness
Open

Refuse a mutable proxy_cast on a const contained value#80
mingxwa wants to merge 1 commit into
ngcpp:feature/v5from
mingxwa:user/mingxwa/proxy-cast-constness

Conversation

@mingxwa

@mingxwa mingxwa commented Sep 7, 2026

Copy link
Copy Markdown
Member

Changes

  • Fixed the constness guard in proxy_cast_dispatch to test the referenced type rather than the reference, so a const contained value is no longer reachable mutably.
  • Added TestIndirectCast_Ref_ConstTarget and TestIndirectCast_Ptr_ConstTarget, covering the value form and the pointer form.

proxy_cast<int&> on a proxy whose contained value is const now throws bad_proxy_cast, and the pointer form returns nullptr, which is what the const qualified overload already promised. Reaching a const contained value still works by asking for it as const.

Resolves #79

proxy_cast_dispatch guarded a reference result with std::is_const_v<T>,
where T is the deduced operand type and is therefore always a reference.
A reference type is never const, so the guard never fired and a caller
could ask a const proxy for a mutable reference to what it contains and
get one, silently casting the constness away.

The guard now tests the referenced type. proxy_cast<int&> on a proxy
whose contained value is const throws bad_proxy_cast, and the pointer
form returns nullptr, which is what the const-qualified overload already
promised. Reaching a const contained value still works by asking for it
as const.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant