I'm a bit confused about type conversions in this exercise so I hope someone will enlighten me. C++ is a statically typed language therefore types must match otherwise conversion rules must be provided. This exercise illustrate how to preserve type informations using the function template std::forward, for better understanding I played with the code changing it to test different scenarios and simplifying I wrote this code:std::forward return a rvalue reference.Code:#include <iostream> #include <utility> using namespace std; template <typename F, typename T1, typename T2> void flip(F f, T1 &&t1, T2 &&t2) { // f(t2, t1); // error: it calls g(int, int) f(forward<T2>(t2), forward<T2>(t1)); } void g(int&& i, int&& j) {cout << i << " " << j << endl; } int main() { int z(0); flip(g, 42, z); g(42,42); return EXIT_SUCCESS; }
1) "&&i" isn't bound to "t2" (a lvalue) but to a rvalue reference of "z" therefore any further use of "z" value must be avoided.
2) "&&i" is bound to the rvalue reference of std::forward therefore any further use of "t2" must be avoided ("z" lvalue not moved). And the same applies to "t1"
Which of these two answers is correct?
Thank you in advance