C++C++11C++14C++17Learn C++Templates

Learn How To Use Non-Copyable Movable Types in C++ Templates

Learn How To Use Non Copyable Movable Types in C++ Templates

In C++, memory and CPU/GPU management are very important and the compiler is amazingly using them well in templates, classes, and objects. Every declaration and usage of any bits may cause a lot of heavy calculations, memory usage, and high CPU/GPU usage. Using copy and move types in templates is very important when you develop a professional app. In this post, we explain how you can use non-copyable movable types in C++ templates.

Understanding non-copyable but movable types in C++ templates

In C++, types that are not copyable, such as ones that use unique_ptr, can be made movable. Although they cannot define assignment operators, they can implement move functions and swap functions, since these do not require copying when rvalue references are used. A sort function could be developed, since it only requires swapping, not copying.

For instance, consider this Tx function that takes one argument:

However,  a compiler error occurs when a T is used whose constructor’s parameter is a non-const reference. You can fix this case by removing const from the definition:

However, the previous example now fails: This causes an error, because the value causes the template argument to be matched to int &, but this does not bind to the rvalue .

This can be remedied by defining a factory function for each case of const and non-const. This is however problematic, because the number of functions needed increases exponentially with the number of arguments.

Now, let’s see how we can do this.

Is there a simple example of non-copyable but movable types in C++ templates?

If you make the argument an rvalue reference, you can simplify the situation:

Now the argument u binds to both rvalues and lvalues. The forward function returns an rvalue or lvalue, exactly as it was passed. It can be defined in this way:

Is there a full example of non-copyable but movable types in C++ templates?

Here is a full C++ example that combines all of the above.

For more information on this feature, check this link and see Rvalue references for *this Proposal document.

Learn How To Use Non Copyable Movable Types in C++ Templates 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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++17Language FeatureLearn C++

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

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

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

How To Use The SVG Image Format With Skia In C++ Builder

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

How To Use Skia Images in C++ Builder?