Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't std::any_cast support implicit conversion?

Tags:

c++

c++17

Why does std::any_cast throw a std::bad_any_cast exception when an implicit conversion from the actual stored type to the requested type would be possible?

For example:

std::any a = 10;  // holds an int now auto b = std::any_cast<long>(a);   // throws bad_any_cast exception 

Why is this not allowed and is there a workaround to allow an implicit conversion (in case the exact type that std::any holds is unknown)?

like image 202
Timo Avatar asked Mar 22 '18 11:03

Timo


People also ask

Is implicit type conversion bad?

Implicit conversions allow the compiler to treat values of a type as values of another type. There's at least one set of scenarios in which this is unambiguously bad: non-total conversions. That is, converting an A to a B when there exists A s for which this conversion is impossible.

What are implicit conversions C++?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.


1 Answers

std::any_cast is specified in terms of typeid. To quote cppreference on this:

Throws std::bad_any_cast if the typeid of the requested ValueType does not match that of the contents of operand.

Since typeid doesn't allow the implementation to "figure out" an implicit conversion is possible, there's no way (to my knowledge) that any_cast can know it's possible either.

To put it otherwise, the type erasure provided by std::any relies on information available only at run-time. And that information is not quite as rich as the information the compiler has for figuring out conversions. That's the cost of type erasure in C++17.

like image 106
StoryTeller - Unslander Monica Avatar answered Sep 20 '22 08:09

StoryTeller - Unslander Monica