Skip to main content

Move semantics



For a long time in C++ was no fast way to deal with return value. They were copied and there was no hope to change it. Some have used return parameters in argument list like:

void GetObjectsInArea(Area exactArea, ObjectList& objList);

To avoid unnecessary copies and speed up the execution developers were doing a lot of work. Eventually when optimized code were becoming unreadable the developers would abandon it and the code would transform into legacy code at some point.


Fortunately we've passed this time and nowadays C++ gets stronger and stronger than it was ever before. Before defining standards the community got RVO and a little bit later we've got NRVO, which allows us to write such code that previously would force creating of a copy. The C++11 standard introduced to us move semantics. It helped to speed up code even without changing a single line of code in various projects.

std::move helps us with passing some big data structures between functions. Helps us obtain ownership of data in objects which it belongs. You able to create objects once during flow of you application and translate them from a module to a module where they belongs.

C++11 introduced some other implicit class operators such as move constructor and move assign operator which allows your custom types to be moved from instance to instance.


The topic of this articles have to describe what rvalue, lvalue and others mean, but it appeared to be big enough topic for other article. Well it should be mentioned that copy elision that appeared in C++17 plays important role in optimizing copies.

Memory handling always was an important topic in C++ and move semantics took it's place in whole bunch of various optimizations and improvements.

Writing this article I was planning on write a lot of samples but unfortunately it heavily depends on compilers and takes a lot of sub-topics. So this one isn't easy to describe in some simple samples, instead you need to dig dipper into it, cause it's really interesting and quite important to understand this concept.

Popular posts from this blog

Extend your list of debug tools with Sanitizers

How many ways to check if code is OK do you know? Probably the one would make a list with debuggers, logging, static/dynamic checkers, profilers and some other tools. The tools the one has in the list are there cause they've been checked with a time. They don't cover all the possible cases of mistakes that could be made during coding phase of a project, but they cover necessary minimum. Today we will try to extend our tool set with a young tools that already got into clang and if you have clang on your machine you can give it a try.

Going up step by step

Have you ever wanted to be able to iterate through your own class as if it was a cool STL container?