#include <vector>

int main() {
    std::vector{1,2,3} = std::vector{3,2,1};

This code has been compiling for the last several years, despite the fact we have the perfect tool to make this foolish code stop compiling. Adding an ampersand at the end of every copy-assignment and move-assignment operator (or operators of the form <op>=).

Why isn’t this best practice already?

Or if it is, why isn’t it applied to new (and old?) libraries?

Update (2022-02-11 8:00AM CET): I also posed this question on CppLang slack and I received a link to a discussion on Cpp Core Guidelines GitHub. The short discussion can be reduced to a single sentence (the notable opinion of one of the participants, and an important voice in the committee): “I don’t think the advantage is important enough to bother”, which is probably correct. I wonder if the opinion would have been different if we had this language feature earlier in the development of C++.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.