dimanche 4 octobre 2015

rvalue reference matching (perfect forwarding example)

I got confused by the following perfect forwarding function, where the template parameter T can match rvalue or lvalue references:

template<typename T>
void foo(T&& t){
    T::A; // intended error to inspect type
}

int main(){
    std::vector<int> a;
    std::vector<int> && b = std::move(a);

    foo(b);             // T is std::vector<int> & 
    foo(std::move(a));  // T is std::vector<int>
}

I dont understand why the template argument deduction of T in foo is so different in these two cases? Whats the fundamental difference and important what is t's type in function foo.

std::move(a) returns a rvalue reference and b is already a rvalue reference (but has a name).

Is that right that, b s type is a rvalue reference to std::vector<int>, but as far as my understanding goes, it has a name and is thus considered an lvalue in function main?

Can anyone shine some light into this :-)

Aucun commentaire:

Enregistrer un commentaire