简体   繁体   中英

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 .

In that documentation the Methods and operators table states, that the expression a = b has the post condition of 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

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.

What am I missing here?

The cppreference page is abridged and adapted from the standard for readability. 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.

Here is what the (current draft) standard says:

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

For move-assignment:

Expression: a = rv (where rv is a non-const rvalue)
Return type: X&
Operational Semantics: All existing elements of a are either move assigned to or destroyed
Ensures: a is equal to the value that rv had before this assignment
Complexity: linear

For copy-assignment:

Expression: r = a
Return type: X&
Ensures: r == a
Complexity: linear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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