Copy/move std::vector::erase() 和 std::deque::erase() 中的赋值
Copy/move assignment in std::vector::erase() and std::deque::erase()
在回答 的过程中,我偶然发现 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
,明确提到调用了移动赋值运算符。
还要求 T
为 MoveAssignable
,以便 erase()
与 std::vector
和 std::deque
一起使用 (Table 100) , 但这并不意味着移动赋值运算符的存在:可以定义一个复制赋值运算符,而不定义移动赋值运算符,并且这个 class 将是 MoveAssignable
.
为了以防万一,我检查了 GCC 和 Clang,如果没有移动赋值运算符,std::vector::erase()
确实调用了复制赋值运算符,std::deque::erase()
也是如此(DEMO) .
所以问题是:我是不是遗漏了什么,或者这是标准中的(编辑)问题?
更新:
我已经提交了 LWG issue #2477.
在 Lenexa 会议上 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
的措辞也将是
澄清一点。
在回答 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 ofT
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
,明确提到调用了移动赋值运算符。
还要求 T
为 MoveAssignable
,以便 erase()
与 std::vector
和 std::deque
一起使用 (Table 100) , 但这并不意味着移动赋值运算符的存在:可以定义一个复制赋值运算符,而不定义移动赋值运算符,并且这个 class 将是 MoveAssignable
.
为了以防万一,我检查了 GCC 和 Clang,如果没有移动赋值运算符,std::vector::erase()
确实调用了复制赋值运算符,std::deque::erase()
也是如此(DEMO) .
所以问题是:我是不是遗漏了什么,或者这是标准中的(编辑)问题?
更新: 我已经提交了 LWG issue #2477.
在 Lenexa 会议上 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 ofT
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 themoveassignment operator ofT
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
的措辞也将是
澄清一点。