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