为什么std :: forward_list ::: remove和std :: erase <std :: forward_list>具有不同的值类型?

xmllmx

std::forward_list具有成员函数

size_type remove(const T& value);

和一个非成员函数 std::erase声明如下:

template<class T, class Alloc, class U>
typename forward_list<T, Alloc>::size_type
    erase(forward_list<T, Alloc>& c, const U& value);

请注意,非成员函数;std::erase类型不同而成员函数仅将type用于UvalueremoveTvalue

为什么std::erase不只将typeT用于value

不一致背后是否有任何设计依据?

讲故事的人-Unslander Monica

我可以看到一个实际的原因。这与模板自变量推导带来的困难有关。想象一下这个假设功能:

template<typename T>
void foo(std::forward_list<T> const&, T const&) {}

您从此调用中得到什么?

std::forward_list<double> l;
foo(l, 1);

答案是在模板参数推导中出现错误。根据一个参数T是double,但是根据另一个参数,它是int。如果我要编写代码erase,那么我也将使用两个不同的模板参数,以避免在纯真的代码中出现此类问题。

现在remove不是成员模板,它是任何专业化的常规成员函数。因此,您可以毫无问题地编写:

std::forward_list<double> l;
// later
l.remove(1);

1是一个整数,它不匹配的doubleremove期待。但是,这不是问题。因为remove是具体专业化的常规成员函数,所以隐式转换是可能的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++ STL-为什么std :: forward_list没有size()方法?

对std :: forward_list :: remove_if谓词的要求

std :: forward_list和序列概念要求

如何删除存储在std :: forward_list中的动态元素

O(1)之后std :: forward_list的emplace_如何?

为什么`return {};`不适用于`std :: forward_list`?

在C ++ 11中std :: list :: splice()和std :: forward_list :: splice_after()是否声明为noexcept?

std :: forward_list是否支持手动重新指向?

您可以在遍历std :: forward_list时将其删除吗?

是否在空std :: forward_list中定义了before_begin()之后的元素?

如何在不使用任何循环的情况下将 std::vector 或数组插入 std::forward_list?

为什么没有std :: erase?

list和forward_list性能之间的区别?

为什么std :: forward()不演绎类型?

为什么std :: list具有remove和remove_if函数

带有指向自身的指针的 forward_list 结构

std :: move和std :: forward之间有什么区别

为什么要使用std :: forward?

为什么不总是使用std :: forward?

为什么在概念中使用std :: forward?

为什么std :: list具有最大大小?

为什么std :: forward将左值和右值转换为右值引用?

xlc是否支持forward_list

何时使用C ++ forward_list

std :: forward和operator()

为什么C ++ 11中的std :: initializer_list重载了std :: begin()和std :: end()?

使用std :: copy复制std :: list并使用std :: list :: erase删除

std :: forward_list.push_front(std :: thread)无法编译

为什么 std::initializer_list 不支持 std::get<>、std::tuple_size 和 std::tuple_element