vendredi 28 février 2020

Passing move-only objects as r-value reference parameters vs no-reference parameters - what is the difference?

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));
}

Aucun commentaire:

Enregistrer un commentaire