Skip to content

The ValueType parameter of std::any_cast is confusingly named #1202

@jwakely

Description

@jwakely
template<class ValueType>
  ValueType any_cast(const any& operand);
template<class ValueType>
  ValueType any_cast(any& operand);
template<class ValueType>
  ValueType any_cast(any&& operand);

This template parameter is confusing, because it's really the return type of the function, not the type of the contained value in the any (as shown by the fact the requirements say it must be a reference, or copy constructible). When the parameter is a reference the functions return by reference, not by value, and ValueType is not the type of the value in the any. The parameter can be const but the value in the any is not a const-qualified type.

Elsewhere in the library "value type" is used for containers, iterators, and allocators, and must be an object type, not a reference type.

I suggest renaming this to make it clearer that it can be a reference, and is not the type of the value in the any. Result or ResultType might be better. Or just Type or T.

For the any_cast overloads taking pointers the ValueType parameter is OK, as in that case it does refer to the type of the value in the any.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions