gpt4 book ai didi

c++ - 在 std::vector::erase() 和 std::deque::erase() 中复制/移动赋值

转载 作者:IT老高 更新时间:2023-10-28 11:32:33 26 4
gpt4 key购买 nike

在回答过程中another question我偶然发现 std::vector::erase()std::deque::erase() 的措辞略有不同。

这就是 C++14 关于 std::deque::erase 的说法([deque.modifiers]/4-6,重点是我的):

Effects: ...

Complexity: The number of calls to the destructor is the same as the number of elements erased, but The number of calls to the assignment operator is no more than the lesser of the number of elements Before the erased elements and the number of elements after the erased elements.

Throws: Nothing unless an exception is thrown by the copy constructor, move constructor, assignment operator, or move assignment operator of T.

这是关于 std::vector::erase ([vector.modifiers]/3-5) 的内容:

Effects: ...

Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the move assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.

Throws: Nothing unless an exception is thrown by the copy constructor, move constructor, assignment operator, or move assignment operator of T.

如您所见,它们的异常规范是相同的,但是对于 std::vector 明确提到调用了移动赋值运算符。

还要求 TMoveAssignable 以便 erase()std::vector 一起工作> 和 std::deque (表 100),但这并不意味着存在移动赋值运算符:可以定义复制赋值运算符,而不定义移动赋值运算符,并且此类将是 MoveAssignable

以防万一,我检查了 GCC 和 Clang,如果没有移动赋值运算符,确实 std::vector::erase() 调用复制赋值运算符,而 std::deque::erase() 做同样的事情 (DEMO)。

所以问题是:我是否遗漏了什么,或者这是标准中的(编辑)问题?

更新:我已经提交了 LWG issue #2477 .

最佳答案

在 Lenexa session 上 got Immediate status建议的决议:

This wording is relative to N4296.

Change 23.3.3.4 [deque.modifiers]/5 to:

-5- Complexity: The number of calls to the destructor of T is the same as the number of elements erased, but the number of calls to the assignment operator of T is no more than the lesser of the number of elements before the erased elements and the number of elements after the erased elements.

Change 23.3.6.5 [vector.modifiers]/4 to:

-4- Complexity: The destructor of T is called the number of times equal to the number of the elements erased, but the move assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements.

也就是说,如果解决方案被接受,则不会特别提及 std::vector::erase 的移动分配,以及 std::deque 的措辞: :erase 将是澄清一点。

关于c++ - 在 std::vector::erase() 和 std::deque::erase() 中复制/移动赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28277982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com