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

Allowing move constructors to throw with std::move_if_noexcept

Allowing move constructors to throw with stdmove if noexcept

In a modern C++ code compiler, there are many features that help you learn, master, and remember the various features and functions of the C++ language. One such feature of modern C++ is the move constructor that allows you to move the resources from one object to another without copying them. The C++11 standard provides an alternative to std::move, which is std::move_if_noexcept, to solve some problematic interactions between move constructors, templates, and certain standard library member functions. In this post, we explain what the move constructor is, what is std::move_if_noexcept, and how we allow move constructors to throw [noexcept] in C++.

First, let’s remind ourselves what is the move constructor in C++.

What is a move constructor in C++?

The move constructor is a constructor that allows you to move the resources from one object to another object without copying them. The move constructor allows you to move the resources from an rvalue object into to an lvalue object. Here is the most common syntax for the move constructor in C++.

and this is how you can create a move constructor in a class.

As shown 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. We can use move constructor with std::move as in example below.

Now, let’s see what is std::move_if_noexcept in C++.

What is std::move_if_noexcept in C++?

The C++11 standard provides an alternative to std::move, which is std::move_if_noexcept, to solve some problematic interactions between move constructors, templates, and certain standard library member functions.

The move_if_noexcept is an alternative to std:: move in the standard library that combines move semantics with a strong exception guarantee. The std::move_if_noexcept returns an rvalue reference to the argument unless copying is a better option than moving to provide at least a strong exception guarantee. In other words, the move_if_noexcept is used to determine whether a move or a copy should be done. It returns an rvalue reference to the argument if its move constructor does not throw exceptions or if there is no copy constructor (move-only type), otherwise obtains an lvalue reference to its argument.

Here is the general header definition located in <utility> library (since C++11),

When we use std::move_if_noexcept(x) , it grants permission to move x unless it would throw (cause) an exception.

Is there a simple example of how to use std::move_if_noexcept in C++?

Here is a simple about move_if_noexcept in C++.

Here, note that a simple empty C++ class is perfectly equivalent to default implementations in a class (see Rule of Six).

Now, let’s see what is happening in a full example.

Is there a full example of how to use std::move_if_noexcept in C++?

Here is a full example of how to use move_if_noexcept in C++.

Here is the output of move and move_if_noexcept usage:

For more information on this feature, see Allowing move constructors to throw Proposal document.

Allowing move constructors to throw with stdmove if noexcept 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++?