Rvalue - Lvalue

Why this is true:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void f (const int& x) {std::cout << "[lvalue]";}
void f (int&& x) {std::cout << "[rvalue]";}

template <class T> 
void func (T&& x) 
{
  f (x);                   
  f (std::forward<T>(x));  
}

int main()
{
  int a;
  func (a);
  func (0);
}


and why this is wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void f (const int& x) {std::cout << "[lvalue]";}
void f (int&& x) {std::cout << "[rvalue]";}

void func (int&& x) 
{
  f (x);                   
  f (std::forward<int>(x));  
}

int main()
{
  int a;
  func (a);
  func (0);
}


in both cases, in the first call, I'm going to func a value lvalue while func requires an rvalue reference..
You have to use std::forward in second case to pass a.
In previous version, you actually have two different functions for a and for 0.
Topic archived. No new replies allowed.