Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jwakely opened this issue Dec 9, 2016 · 3 comments
Closed

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

jwakely opened this issue Dec 9, 2016 · 3 comments
Assignees

Comments

@jwakely
Copy link
Member

jwakely commented Dec 9, 2016

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.

@timsong-cpp
Copy link
Contributor

any's member functions (ctor, =, emplace) have similar problems. The actual type of the value being stored is the decayed type, not actually ValueType, despite the name.

The pointer variant of any_cast also has issues - ValueType can be cv-qualified (the comparison is by typeid which drops cv-qualification), but the value being stored is not.

@jensmaurer
Copy link
Member

I'm preparing a patch that replaces ValueType with T and uses VT where the description seems to hint we're dealing with the actual ValueType.

@jensmaurer jensmaurer self-assigned this Dec 13, 2016
@jwakely
Copy link
Member Author

jwakely commented Dec 13, 2016

That sounds good to me - thanks.

jensmaurer added a commit to jensmaurer/draft that referenced this issue Dec 13, 2016
The parameter has a confusing name: It is not actually the value type
contained in the 'any'.
Where the new T clashes with an existing use of T, rename the latter to VT,
since this is actually the value type of the 'any'.

Fixes cplusplus#1202.
jensmaurer added a commit to jensmaurer/draft that referenced this issue Dec 13, 2016
The parameter has a confusing name: It is not actually the value type
contained in the 'any'.
Where the new T clashes with an existing use of T, rename the latter to VT,
since this is actually the value type of the 'any'.

Fixes cplusplus#1202.
tkoeppe pushed a commit that referenced this issue Dec 13, 2016
The parameter has a confusing name: It is not actually the value type
contained in the 'any'.
Where the new T clashes with an existing use of T, rename the latter to VT,
since this is actually the value type of the 'any'.

Fixes #1202.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants