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

后端 未结 5 963
傲寒
傲寒 2020-12-24 12:33

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 p

5条回答
  •  余生分开走
    2020-12-24 13:24

    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.

提交回复
热议问题