简体   繁体   English

C++ STL 容器要求 - 后置条件混淆

[英]C++ STL Container requirements - Post condition confusion

I am currently looking at implementing a custom container which needs to be compatible with STL algorithms and therefore must meet the C++ container requirements as described here .我目前正在考虑实现一个自定义容器,该容器需要与 STL 算法兼容,因此必须满足此处所述的 C++ 容器要求。

In that documentation the Methods and operators table states, that the expression a = b has the post condition of a == b .在该文档中, Methods and operators表指出,表达式a = b具有a == b的后置条件。 I am heavily confused by this.我对此感到非常困惑。 As the table entry for this expression states:正如此表达式的表条目所述:

destroys or move-assigns all elements of a from elements of b毁坏,或移动-受让人所有元素a从元素b

To my understanding, moving an object comes with the expectation that the moved object (source object?) will be left in a valid but undefined state.据我了解,移动一个对象伴随着期望被移动的对象(源对象?)将处于有效但未定义的状态。 Therefore, the condition a == b cannot be met in my opinion.因此,我认为不能满足条件a == b

What am I missing here?我在这里缺少什么?

The cppreference page is abridged and adapted from the standard for readability. cppreference 页面是从标准中删节和改编的,以提高可读性。 In this case it oversimplifies the requirements, which have to distinguish between the different value categories (as you correctly reasoned).在这种情况下,它过度简化了要求,这些要求必须区分不同的值类别(正如您正确推理的那样)。 Or maybe the two separate assignment cases (which do not appear consecutively in the table in the standard) have been merged by accident.或者,两个单独的分配案例(在标准的表格中没有连续出现)可能被意外合并。 Either way, cppreference is currently wrong.无论哪种方式, cppreference 目前都是错误的。

Here is what the (current draft) standard says:这是(当前草案)标准所说的:

http://eel.is/c++draft/containers#container.requirements.general-4 http://eel.is/c++draft/containers#container.requirements.general-4

For move-assignment:对于移动分配:

Expression: a = rv (where rv is a non-const rvalue)表达式: a = rv (其中rv是非常量右值)
Return type: X&返回类型: X&
Operational Semantics: All existing elements of a are either move assigned to or destroyed操作语义:现有的所有元素a都上移分配或销毁
Ensures: a is equal to the value that rv had before this assignment确保: a等于rv在此赋值之前的值
Complexity: linear复杂度:线性

For copy-assignment:对于复制分配:

Expression: r = a表达式: r = a
Return type: X&返回类型: X&
Ensures: r == a确保: r == a
Complexity: linear复杂度:线性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM