1

Move-only objects can be passed into functions as r-value reference parameters and as regular no-reference parameters. What is the difference between these two cases?

See noRValueReference and rValueReference in the following example:

struct MoveOnly {
  MoveOnly() = default;
  MoveOnly(MoveOnly&&) = default;
  MoveOnly& operator=(MoveOnly&&) = default;
  MoveOnly(const MoveOnly&) = delete;
  MoveOnly& operator=(const MoveOnly&) = delete;
};

void noRValueReference(MoveOnly val) {}
void rValueReference(MoveOnly&& val) {}

int main() {
    MoveOnly a, b;
    noRValueReference(std::move(a));
    rValueReference(std::move(b));
}
user835943
  • 171
  • 9
  • For one thing, one calls the move constructor and the other does not. [Demo](https://rextester.com/KJCT78291) – Igor Tandetnik Feb 29 '20 at 02:18
  • https://stackoverflow.com/a/24828204/9363996 this answer mentiones that a compiler in the case of ```noRValueReference``` call will look into the qualifiers of an object. So it will use a copy-constructor in case lvalue has been passed and a move-constructor for an rvalue object. I can't find an "official" explanation for this, so I don't post it as an aswer. But I could, if you consider it to be enough. THough I myself am very interested in the topic. – Sergey Kolesnik Mar 06 '20 at 15:55

0 Answers0