相关疑难解决方法(0)

std::any_cast() 和 std::get_if(std::variant) 是否绝对有必要将指针作为参数?

对于std::anystd::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界面?

c++ c++17

5
推荐指数
1
解决办法
952
查看次数

标签 统计

c++ ×1

c++17 ×1