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

OpenGL: how to start studying?

I've been studying OpenGL for about 2 years, but I haven't got far with it and had no idea what is wrong. Is it me unable to understand OpenGL abstractions or is it designed not very well? I was looking for an answer all these years when suddenly I've started getting into math since my job required some math skills and suddenly OpenGL started to make sense for me. I ain't trying to say that now it's fun and games - honestly I find it challenging, but not as bad as before.

Embrace the changes

“Change is the law of life. And those who look only to the past or present are certain to miss the future.” —John F. Kennedy I want to write a couple things on changes that happen all the time and which we don't notice or choose to do so. Not sure what they teach children in your country but in Belarus of 90-00s there was not much interest in a child that had some interests in topic that was out of school programs. And it made me into self-harming youngster who didn't know what to do and where to go. For some times I felt like there were no changes around me, which was faulty way of thinking. So that approach rooted in me until I've got into a University. People there had a lot of interests so different of mine that all first year I've been stunned. It was so hard for me to get it - people, that have will, do embrace changes and adapt to it to go further with their plans.

What was so alien for me back then, became a most important force in my life now. To be able to mo…

Tuples: are they useful?

In some languages there is such a thing as tuple which makes real to write such code (this time it's in python):

def f(x):
 # do stuff return (True, modified_string)

success, modified_string = f(something) 
C++ hadn't such feature until C++11. That was one of the reasons to pass variable as referenced argument, which ended up creating huge code bases that have unused arguments in it's functions. That happened in order to save revers compatibility and allow clients to use stable interfaces to access libraries without checking the version of dynamically attached library in order to use correct functions.