Skip to Content
Advanced C++ Programming Cookbook
book

Advanced C++ Programming Cookbook

by Dr. Rian Quinn
January 2020
Intermediate to advanced
454 pages
11h 25m
English
Packt Publishing
Content preview from Advanced C++ Programming Cookbook

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern C++ Programming Cookbook

Modern C++ Programming Cookbook

Marius Bancila

Publisher Resources

ISBN: 9781838559915Supplemental Content