Title
std::any_cast should never return a cv-qualified type
Status
new
Section
[any.nonmembers]
Submitter
Casey Carter

Created on 2020-04-02.00:00:00 last changed 1 month ago

Messages

Date: 2020-04-24.18:46:57

Proposed resolution:

This wording is relative to N4861.

  1. Modify [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);
    […]
    
  2. Modify [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 R be the type remove_cv_t<T>, and U be the type remove_cvref_t<T>.

    -5- Mandates: For the first overload, is_constructible_v<TR, const U&> is true. For the second overload, is_constructible_v<TR, U&> is true. For the third overload, is_constructible_v<TR, U> is true.

    -6- Returns: For the first and second overload, static_cast<TR>(*any_cast<U>(&operand)). For the third overload, static_cast<TR>(std::move(*any_cast<U>(&operand))).

    -7- Throws: bad_any_cast if operand.type() != typeid(remove_reference_t<T>).

Date: 2020-04-18.00:00:00

[ 2020-04-18 Issue Prioritization ]

Priority to 3 after reflector discussion.

Date: 2020-04-02.00:00:00

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.

History
Date User Action Args
2020-04-18 12:17:54adminsetmessages: + msg11222
2020-04-04 20:26:42adminsetmessages: + msg11196
2020-04-02 00:00:00admincreate