I have a code:
void f(int&& i) {
auto lambda = [](int&& j) { (void)j; }
lambda(i);
}
int main() {
f(5);
}
Clang++ gives an error: no known conversion from 'int' to 'int &&' for 1st argument
Why the i
changes its type to int
when being passed to the lambda()
?
i
which has typeint&&
, and the expression writteni
that is the result of evaluating that variable which has typeint
and value categorylvalue
. It's easy to conflate the two when we represent both with the same syntax. The same sort of thing happens withint i
(variable of typeint
, lvalue expression of typeint
) andint& i
(variable of typeint&
, lvalue expression of typeint
), but the difference between the type of the variable and the type of the expression that evaluates it becomes more important with rvalue refs. – Coaleri
means different things in different contexts. Rvalue references are hard to teach. – Coaler