This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.
std::any_cast
should never return a cv-qualified typeSection: 22.7.5 [any.nonmembers] Status: New Submitter: Casey Carter Opened: 2020-04-02 Last modified: 2020-09-06
Priority: 3
View all other issues in [any.nonmembers].
View all issues with New status.
Discussion:
The return type of the non-pointer overloads of std::any_cast<T>
is T
. This is silly
when T
is cv-qualified (and, since merging P1152
"Deprecating volatile" into the working draft, deprecated when T
is volatile
-qualified).
We should strip cv-qualifiers to determine the type returned.
[2020-04-18 Issue Prioritization]
Priority to 3 after reflector discussion.
Proposed resolution:
This wording is relative to N4861.
Modify 22.7.2 [any.synop] as indicated:
[…] template<class T> remove_cv_t<T> any_cast(const any& operand); template<class T> remove_cv_t<T> any_cast(any& operand); template<class T> remove_cv_t<T> any_cast(any&& operand); […]
Modify 22.7.5 [any.nonmembers] as indicated:
template<class T> remove_cv_t<T> any_cast(const any& operand); template<class T> remove_cv_t<T> any_cast(any& operand); template<class T> remove_cv_t<T> any_cast(any&& operand);-4- Let
-5- Mandates: For the first overload,R
be the typeremove_cv_t<T>
, andU
be the typeremove_cvref_t<T>
.is_constructible_v<
isTR, const U&>true
. For the second overload,is_constructible_v<
isTR, U&>true
. For the third overload,is_constructible_v<
isTR, U>true
. -6- Returns: For the first and second overload,static_cast<
. For the third overload,TR>(*any_cast<U>(&operand))static_cast<
. -7- Throws:TR>(std::move(*any_cast<U>(&operand)))bad_any_cast
ifoperand.type() != typeid(remove_reference_t<T>)
.