How it works...

C++ added a function called std::move_if_noexcept(). This function will cast as an r-value if the move constructor/assignment operator cannot throw, and will cast as an l-value otherwise. For example, take a look at the following code:

#include <iostream>struct the_answer_noexcept{    the_answer_noexcept() = default;    the_answer_noexcept(const the_answer_noexcept &is) noexcept    {        std::cout << "l-value\n";    }    the_answer_noexcept(the_answer_noexcept &&is) noexcept    {        std::cout << "r-value\n";    }};

To try this, we will perform the following steps:

  1. First, we will create a class that has a move/copy constructor that cannot throw:
struct the_answer_can_throw{    the_answer_can_throw() = default; the_answer_can_throw(const the_answer_can_throw ...

Get Advanced C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.