The object-oriented programming features of modern C++ is really enhanced with many features, such as Classes, Objects, constructors, move constructors, copy constructors, destructors, etc. Since the C++11 standard was released one of the modern programming 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 Eligible Move Constructor and, in this post, we explain what is an eligible move constructor in modern C++.
Table of Contents
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,
1 2 3 4 5 6 7 |
class Tmyclass { public: std::string str; }; |
Then we can create our objects with this Type of myclass as below.
1 2 3 |
Tmyclass o1, o2; |
Now, let’s 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 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),
1 2 3 |
class_name ( class_name && ) |
this general syntax is also a syntax for the “Typical declaration of a move constructor” as in below,
1 2 3 4 5 |
class_name ( class_name && ) // Declaration { // Definition } // Definition |
What is an eligible move constructor in modern C++?
Since C++11, the Eligible Move Constructor is a Move Constructor which is eligible if it is not deleted. This definition is changed after C++20, the Eligible Move Constructor is a Move Constructor which is eligible if it is not deleted, if it has any associated constraints that are satisfied, if it has no move constructor with the same first parameter type is more constrained.
Until C++20, the move constructor is eligible:
- if it is not deleted
Since C++20, the move constructor is eligible:
- if it is not deleted,
- and if it has any associated constraints that are satisfied
- and it has no move constructor with the same first parameter type that is more constrained.
In modern C++ programming, the triviality of eligible move constructors is important because it determines whether the class is a trivially copyable type and whether the class is an implicit-lifetime type.
Is there a simple example of an eligible move constructor in modern C++?
In modern C++ example, this simple Tx
class has a move constructor.
1 2 3 4 5 6 7 8 |
class Tx { public: std::string str = "LearnCPlusPlus.org"; }; |
This class has an eligible move constructor because it has a default move constructor that compiler automatically defines, in Modern C++ it is similar as below.
1 2 3 4 5 6 7 8 9 10 11 |
class Tx { public: std::string str = "LearnCPlusPlus.org"; Tx() = default; // Constructor Tx(Tx&& other) = default; // Move Constructor }; |
Now, we can define a new Ty
class and we can use one of Tx
classes above as a base class here.
1 2 3 4 5 6 7 8 9 |
class Ty : public class Tx // uses Tx as a base class { public: void print_str(){ std::cout << str << std::endl; } // This class has Eligible Constructor from Tx class }; |
This new Ty
class above has an eligible move constructor from Tx
class. Because it is not deleted in base class or in this class.
Is there a full example of an eligible move constructor in modern C++?
Here is a full example that uses an eligible move constructor from a simple class from a class with a base class, and from a template class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <string> class Ta { public: std::string str = "LearnCPlusPlus.org"; void print_str(){ std::cout << str << std::endl; } // This class has auto-generated move constructor by compiler // that is not deleted (eligible) }; int main() { class Ta a1; class Ta a2 = std::move(a1); // using eligible move constructor system("pause"); return 0; } |
Here is a full example that uses an eligible move constructor from a class with a base class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <string> class Tx { public: std::string str = "LearnCPlusPlus.org"; Tx() = default; // Constructor Tx(Tx&& other) = default; // Move Constructor }; class Ty : public Tx // uses Tx as a base class { public: void print_str(){ std::cout << str << std::endl; } // This class has eligible move constructor from Tx class }; int main() { class Ty o1; class Ty o2 = std::move(o1); // using eligible move constructor from Tx o1.print_str(); o2.print_str(); system("pause"); return 0; } |
Here is a full example that uses eligible move constructor from a template class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <iostream> #include <string> class Tx { public: std::string str = "LearnCPlusPlus.org"; Tx() = default; // Constructor Tx(Tx&& other) = default; // Move Constructor }; template <class T> class myclass { public: T myT; void print_str(){ std::cout << myT.str << std::endl; } }; int main() { myclass<Tx> t1; myclass<Tx> t2 = std::move(t1); // using eligible move constructor from template t1.print_str(); t2.print_str(); system("pause"); return 0; } |
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
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.