|
|
|
|
|
by monk_the_dog
4233 days ago
|
|
| but a more technical understanding requires the knowledge that rvalue references can bind to any l- or rvalue I don't think that's true. rvalue references can only bind to rvalues. Here's a complete program. foo(i) won't compile bacause i is not an rvalue. bar(i) will compile, and foo(1) will compile. #include <iostream> using namespace std; int foo(int&& i){
return i;
} template<class T>
T bar(T&& i){
return i;
} int main()
{
int i=0;
cout << foo(i) << endl; return 0;
}edit: I am replying humbly. C++ is a complex language and I might very well be wrong. |
|
Your code example does not work because you call "foo" instead of "bar". But consider this one:
#include <iostream>
using namespace std;
template<class T> T bar(T&& i){ return std::forward<T>(i); }
int main() {