Site icon Learn C++

What Is A Trivial Move Constructor In Modern C++?

What Is A Trivial Move Constructor In Modern C++

C++ is a wonderful programming language with its object-oriented programming features, such as Classes, Objects, constructors, move constructors, copy constructors, destructors, etc. Since the C++11 standards, in modern C++, 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 Trivial Move Constructor which is defined or defaulted in a base class, and in this post, we explain what is a trivial 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.

[crayon-6763c15637b14017491028/]

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

[crayon-6763c15637b1f749741552/]

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

[crayon-6763c15637b22898131669/]

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

[crayon-6763c15637b2b523210773/]

What is a trivial move constructor in modern C++?

The Trivial Move Constructor is a Move Constructor which is implicitly defined or defaulted and has no virtual member functions, no base classes. The trivial move constructor generally a constructor that comes from template class or base class. The move constructor selected for every direct base of T or for every non-static class type (including array of class type) of T is trivial move constructor.

The move constructor for class T is trivial if all of these below are provided.

and

In general, the trivial move constructor is a constructor that makes a copy of the object representation by using std::memmove, it operates like the trivial copy constructor. Note that, all POD data types (data types compatible with the C language) are trivially movable.

Is there a simple example of a trivial move constructor in modern C++?

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

[crayon-6763c15637b2e763119195/]

This is same as below.

[crayon-6763c15637b30197542371/]

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

[crayon-6763c15637b36418495795/]

As you see, this new Ty class above has a trivial move constructor from Tx class. Because it is implicitly defined or defaulted.

Is there a full example of a trivial move constructor in modern C++?

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

[crayon-6763c15637b39523932977/]

Here is another full example about trivial move constructor with a template class.

[crayon-6763c15637b3b929250693/]

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.

Exit mobile version