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