C++C++11Learn C++

Learn How To Eliminate Unnecessary Copying In C++

Learn How To Eliminate Unnecessary Copying In C++

In modern C++, rvalue references are a compound type like standard C++ references, which are referred to as lvalue references. New rvalue reference rules were set by the C++11 specifications. In many cases, data is copied that simply needs to be moved, that is, the original data holder need not retain the data. The rvalue reference can be used to distinguish cases that require copying versus cases that merely require moving data. In this post, we explain how we use rvalue references to eliminate unnecessary copying in C++.

How to eliminate unnecessary copying in modern C++?

In many cases, data is copied that simply needs to be moved, that is, the original data holder need not retain the data. An example is swapping data in two structures, so neither structure holds its previous data. It would be logically sufficient to simply switch the references to the data.

rvalue references can be used to distinguish cases that require copying versus cases that merely require moving data. Since copying can be a lengthy operation, you want to avoid it, if possible.

If a function wants to copy something that has been passed to it as an rvalue, then it can do a move rather than a copy, because it knows that the value is temporary. If a function is passed an lvalue, it may need to do a full copy if copy elision doesn’t apply. You can distinguish these cases with the function signature.

Consider a class Tx that has a clone function that makes a deep copy of class instances. You want to define a move function that moves an object’s value. This function can be overloaded as follows:

As in the given class example, we can use our specific move function for both rvalues and lvalues as we show below:

Note that the move function for the rvalue parameter does very little, so it executes much more quickly than the move for an lvalue parameter.

We can use a similar technique for functions that need to make copies, such as copy constructors and assignment operators. Suppose we have a template class with a pointer to some other class, where clone is again a deep copy function:

Here, when the copy constructor takes an rvalue:

  • Does a move, not a copy. That is, it simply returns a reference to the data.
  • Treats the rvalue argument pcr just like an lvalue in its code.
  • Leaves the rvalue object in a defined state so that it can safely be deleted.

Is there a full example of how to eliminate unnecessary copying in C++?

Here is the full example which helps explain how to eliminate unnecessary copying in C++

For more information on these kinds of features of rvalue, see Rvalue references for *this Proposal document.

Learn How To Eliminate Unnecessary Copying In C++ The C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17C++20C++23Learn C++Syntax

How To Use std::make_from_tuple in C++ 17

C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?