KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Trying to understand whether using std::forward with auto&& variables is the right way to pass those variables to allow move. Assume there is a function: void moveWidget(Widget&& w); And the caller - two variables to refer to rvalue and lvalue: Widget w; auto&& uniRefLV = w; // lvalue initialiser, // uniRefLV's type is Widget& auto&& uniRefRV = std::move(w); // rvalue initialiser, // uniRefRV's type is Widget&& We know that a variable of type auto&& is a universal reference because there is a type deduction taking place. Which means both uniRefRV and uniRefLV are universal references . In my example it is obvious that uniRefRV is rvalue and uniRefLV is lvalue but conceptually they are both universal references and if definition was different they could represent either rvalue or lvalue . Now, I want to call moveWidget() and perfect forward those universal references types. The guideline (by Scott Meyers) says: Pass and return rvalue references via std::move , universal references via std::forward . And unless I am completely misinterpreting the guideline it seems logical to use std::forward . But let's consider all possible choices: // (1) std::move: moveWidget(std::move(uniRefLV)); // Compiles and looks fine // but violates the guideline? // (unconditionally casts lvalue to rvalue) moveWidget(std::move(uniRefRV)); // Same as above - but not an issue here // as we cast r
Tags (comma-separated)
Save Edits
Cancel