6

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?

Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
  • 4
    Looks like a typo. It should say *copy-assigns all elements of a from elements of b* – NathanOliver Sep 04 '19 at 14:00
  • 1
    b may be r-value result of function call – Andrew Kashpur Sep 04 '19 at 14:01
  • 5
    The STL is the wrong term. It is not the name of the standard library, it's an old library from which many parts were migrated into what is now the standard library. [What's the difference between “STL” and “C++ Standard Library”?](https://stackoverflow.com/questions/5205491/whats-the-difference-between-stl-and-c-standard-library). – François Andrieux Sep 04 '19 at 14:04
  • 2
    @FrançoisAndrieux What would you use if you wanted to refer to the C++ standard library in less than 8 syllables/20 characters? – Max Langhof Sep 04 '19 at 14:44
  • @MaxLanghof If you are under an 8 character constraint, you can probably get away with "stdlib". "the std library" is not very long either. Edit : Misread the end of your comment as "in less than 8 characters?". – François Andrieux Sep 04 '19 at 14:48
  • @MaxLanghof `std::` or "namespace std" depending on if I were writing or talking – Caleth Jan 06 '20 at 09:38

1 Answers1

10

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

Max Langhof
  • 23,383
  • 5
  • 39
  • 72