C++C++11C++14C++17C++20Learn C++Syntax

What Is An Implicitly-declared Move Constructor In Modern C++?

What Is An Implicitly declared Move Constructor In Modern C++

Since the C++11 standards, in modern C++ Programming, one of the features is the move constructor that allows you to move the resources from one object to another object without copying them. One of the move constructors is the implicitly-declared move constructor, which is declared in a base class. In this post we explain the implicitly-declared move constructor in Modern C++.

First, let’s remind ourselves what are 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 objects, and they are user-defined data types that we can use in our program. 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,

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 to an lvalue object.

The move constructor is used to move data of one object to the new one, it is a kind of to make 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 in below,

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

The implicitly-declared move constructor in modern C++ is a move constructor that is declared implicitly by using the move constructor of another base class. In other terms you have a new class that uses the base class, this class has implicitly declared a move constructor from the base class.

If a class type has no move constructors and also there is no copy constructor, copy assignment operator, move assignment operator, or destructor then it will be declared by the compiler. This move constructor will be declared as a default constructor which is a non-explicit inline public member of its class with the signature T::T(T&&). That means you don’t need to declare a move constructor in a new class if not needed. Or you can force the generation of the implicitly declared move constructor by using the default keyword.

In C++, The Rule of Five states that if a type ever needs one of the following special members, then it must have all of the five special members.

  • copy constructor
  • copy assignment
  • move constructor
  • move assignment
  • destructor

So, if you have a move constructor in a class, you should carefully define all of these in accordance with your data members (properties).

Note that, classes may have different move constructors. And, if there is a user-defined move constructor is present, the user may still force the generation of the implicitly declared move constructor with the keyword default.

In addition, Since C++17, the implicitly-declared move constructor has an exception specification by using noexcept specification.

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

Let’s give a simple C++ example of an implicitly-declared move constructor which is a move constructor of other base class. Let’s assume that we have Tx as a base class and we have a new Ty class. This new class can use the move constructor from the Tx.

Here is a Tx class example with a declared and defined move constructor that uses std::move,

As given here above, if you have a move constructor, you should define a Constructor too, otherwise you will have “No matching constructor for initialization of class” error in compilation.

Now, we can define a new Ty class and we can use Tx class as a base class as below.

as you see, this Ty class above has the implicitly-declared move constructor from Tx class. We can use move constructor with std::move as in example below,

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

Here is a full example of a default (forced) move constructor, where one object is moved to another one.

If you run this example, you will see that this str is moved successfully by using an implicitly-declared move constructor from the base class.

Is there a move constructor in a simple class?

Note that, a simple empty C++ class is perfectly equivalent to default implementations (Rule of Five) in a class. A modern compiler is able to provide all these special member functions (default implementations). In example, this simple class below,

is exactly the same as the one below in modern C++.

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 declared 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.

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++20Learn C++

What Are The New Rules For Auto Deduction In C++ 17?

C++C++14C++17Learn C++Syntax

What Are The New Overloads For Ranges In C++ 14?

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

What Are The Member Initializers And Aggregates Features in C++14?

C++C++14C++17Learn C++Syntax

What Is The Integer Sequence (std::integer_sequence) In C++ 14