C++C++11C++14C++17C++20Learn C++SyntaxTemplates

What Is An Implicitly-defined Move Constructor in Modern C++?

What Is An Implicitly defined Move Constructor in Modern C++

The Move Constructor is one of the great features of Object Oriented Programming in C++, such as other features like; copy assignment operator constructors, copy constructors, move assignment operators, destructors, etc. Since the C++11 standards, in modern development, the move constructor allows you to move the resources from one object to another object without copying them. One of the move constructors is the Implicitly-defined Move Constructor which is defined or defaulted in a base class, and in this post, we explain What is Implicitly-defined Move Constructor in Modern C++.

First, let’s remember what are the classes and objects in C++.

What are classes and objects in modern C++?

Classes are defined in C++ using the keyword class followed by the name of the class. Classes are the blueprint for the objects. They are user-defined data types that we can use in our program, and they work as an object constructor. Objects are an instantiation of a class. In C++ programming, most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.

Then we can create our objects with this Type of myclass as below.

Now, lets see what is move constructor,

What is a move constructor in modern C++?

The move constructor is a constructor that allows you to move the resources from one object to another object without copying them. In other words, the move constructor allows you to move the resources from an rvalue object into an lvalue object.

The move constructor is used to move data of one object to the new one, it effectively makes a new pointer to the members of an old object and transfers the resources to the heap memory. When you move a member, if the data member is a pointer, you should also set the value of the member of the old object to a NULL value. When you use the move constructor, you don’t use unnecessary data copying in the memory. This allows you to create objects faster. Mostly, if your class/object has a move constructor, you can use other move methods of other features of C++, for example, std::vector, std::array, std::map, etc. For example, you can create a vector with your class type then you can use the push_back() method that runs your move constructor.

Here is the most common syntax for the move constructor in C++ (since C++11).

This general syntax is also a syntax for the “Typical declaration of a move constructor” as shown below.

What is an implicitly-defined move constructor in modern C++?

The Implicitly-defined Move Constructor is a Move Constructor which is implicitly defined by another base, or it is an implicitly-declared move constructor neither deleted nor trivial.

  • The Implicitly-defined Move Constructor is defined, which means it has a function body with { } that is generated and compiled by the compiler implicitly.
  • The implicitly-defined move constructor performs full move operations on its members if it is a class or struct type.
  • The implicitly-defined move constructor copies the object representation (as in std::memmove) if it is a union type.

According to this paper by Bjarne Stroustrup,

  • By default, an aggregate of elements has an implicitly defined move constructor if every element has a move constructor.
  • An implicitly defined move constructor for an aggregate of elements is explicit if any of its elements have their move constructor explicit. Similarly for a copy constructor

and in addition to these above,

  • From C++11 to C++20, the implicitly-defined move constructor is constexpr if it satisfies the requirements of a constexpr constructor.
  • Since C++23, the implicitly-defined move constructor is constexpr if it satisfies the requirements of a constructor function.

Is there a simple example of an implicitly-defined move constructor in modern C++?

In modern C++ example, this simple class example has a move constructor as default.

This is same like as below.

We can define our own move constructor as shown here:

And, we can use this Tx class as base class to define a new Ty class as below.

As you see, this new Ty class above has Implicitly-defined move constructor from Tx class. Because it is implicitly defined, not deleted and not trivial.

Is there a full example of an implicitly defined move constructor in modern C++?

Here is a full example that uses implicitly defined move constructor from a base class.

Here is another full example of an implicitly defined move constructor with a template class.

If you need more technical details about the move constructor, it is explained by Bjarne Stroustrup and Lawrence Crowlcan in this publication here; https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3053.html

What Is An Implicitly defined Move Constructor in Modern C++ 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++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?