mercredi 10 mars 2021

Aren't forwarding references deduced as r-value references? [duplicate]

I have a specific question regarding forwarding references. (I think) I understand r-value references and std::move, but I have trouble understanding forwarding references:

#include <iostream>
#include <utility>

template <typename T> class TD; // from "Effective Modern C++"

void consume(const int &i) { std::cout << "lvalue\n"; }
void consume(int &&i)      { std::cout << "rvalue\n"; }

template <typename T>
void foo(T&& x) {
//    TD<decltype(x)> xType; - prints int&&
    consume(x);
}

int main() {
    foo(1 + 2);
}

T is int, that's fine. If x is of type int&&, why it prints "lvalue" and we need std::forward ? I mean, where is the conversion from int&& to const int& here?

Aucun commentaire:

Enregistrer un commentaire