Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 312
Joel Bodenmann Avatar asked Sep 04 '19 13:09

Joel Bodenmann


1 Answers

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

like image 198
Max Langhof Avatar answered Oct 13 '22 13:10

Max Langhof