Template allows lvalue to become bound with rvalue reference [duplicate]

  • A+

This question already has an answer here:

This program:

using namespace std; #include <iostream> #include <memory>  struct Dog  {      int legs;  } adog;  Dog gimmeadog() { return adog; }  void walk(Dog && d) { cout << "Nonconst right dog walk/n"; }  //template<class T> void walk(T && d) { d.legs=3; cout << "Nonconst right something walk/n"; }  int main() {     Dog mydog = gimmeadog();     walk(mydog);         return 0; } 

correctly fails to compile on gcc because:

error: cannot bind rvalue reference of type ‘Dog&&’ to lvalue of type ‘Dog’   walk(mydog); 

but if you un-comment the template it happily binds and prints "Nonconst right something walk".

Why? What type is T taking when it works? Is this not defeating the object of rvalue references?


When T is a template argument T&& is a forwarding reference, not a r-value one. T gets deduced as T for r-values and T& for l-values. Such a reference binds to anything.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: