Refuse a mutable proxy_cast on a const contained value - #80
Open
mingxwa wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
proxy_cast_dispatchto test the referenced type rather than the reference, so a const contained value is no longer reachable mutably.TestIndirectCast_Ref_ConstTargetandTestIndirectCast_Ptr_ConstTarget, covering the value form and the pointer form.proxy_cast<int&>on a proxy whose contained value is const now throwsbad_proxy_cast, and the pointer form returnsnullptr, which is what the const qualified overload already promised. Reaching a const contained value still works by asking for it as const.Resolves #79