Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Post Closed as "Duplicate" by Jan Schultke c++
edited tags
Link
juanchopanza
  • 228.2k
  • 35
  • 421
  • 493
Source Link
Shachar Shemesh
  • 8.8k
  • 6
  • 30
  • 65

rvalue reference argument turning into an lvalue

Please consider the following code:

class a { public: int a; }; void func1( a &&var1 ); void func2( a &&var2 ) { func1(var2); } 

When trying to compile it, gcc returns the following:

question.cpp: In function ‘void func2(a&&)’: question.cpp:10:14: error: cannot bind ‘a’ lvalue to ‘a&&’ func1(var); ^ question.cpp:6:6: error: initializing argument 1 of ‘void func1(a&&)’ void func1( a &&var ); ^ 

It seems that var2 is an lvalue, despite it being quite explicitly defined as an rvalue reference. Does the double ampersands lose their meaning once assigned? What is the mechanism that's at work here?