Recently I've been trying to understand move semantics and came up with a question.
The question has already been discussed here.
I implemented the first variant and checked whether it returns l-value or r-value:
#include <iostream>
using namespace std;
template <typename T>
T&& my_forward(T&& x) {
return static_cast<T&&> (x);
}
int main() {
int a = 5;
&my_forward(a); // l-value
return 0;
}
So if I pass l-value, it returns l-value (compiles, because I can take an adress from l-value) and if I do that:
&my_forward(int(5)); // r-value with int&& type
My code doesn't compile, because my_forward returned r-value. In the question above they say that the difference between this implementation and standart one (with std::remove_reference and 2 different arguments with & and && respectively) is that my implementation returns l-value all the time, but as I've shown it returns both r-value and l-value.
So I wonder, why can't I implement std::forward like that? In what specific cases will it show difference between standart one? Also, why should I specify T as a template and can't let it define itself with argument type?
&
usage :)