对于std::any和std::variant我们拥有的功能,要求有关当前包含值的对象,这回nullptr如果请求不能成立时(就像dynamic_cast做):
template<class ValueType>
const ValueType *any_cast(const any *operand);
template<class ValueType>
ValueType *any_cast(any *operand);
Run Code Online (Sandbox Code Playgroud)
和
template <class T, class... Types>
std::add_pointer_t<T> get_if(variant<Types...> *pv);
template <class T, class... Types>
std::add_pointer_t<const T> get_if(const variant<Types...> *pv);
Run Code Online (Sandbox Code Playgroud)
两者都将指针作为参数。为什么?它效率不高。实现每次都检查参数是否不是nullptr。请问nullptr参数使得任何意义可言?
此函数可以是类成员或将引用作为参数(名称可能略有不同)。这种次优设计的原因是什么?只是模拟dynamic_cast界面?