How it works...

In this recipe, we will learn how to use perfect forwarding to ensure that, when we pass parameters in our templates (that is, forward our parameters), we do so in a way that doesn't erase r-valueness. To better understand the issue, let's look at the following example:

#include <iostream>struct the_answer{ };void foo2(const the_answer &is){    std::cout << "l-value\n";}void foo2(the_answer &&is){    std::cout << "r-value\n";}template<typename T>void foo1(T &&t){    foo2(t);}int main(void){    the_answer is;    foo1(is);    foo1(the_answer());    return 0;}

The output is as follows:

In the preceding example, we have two different versions of a ...

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.