Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid useless vector coping by move semantic and using referenceness qualifier #95

Open
xDimon opened this issue Nov 5, 2020 · 0 comments

Comments

@xDimon
Copy link
Member

xDimon commented Nov 5, 2020

For example:

const std::vector<uint8_t> &BlaBla::toBuffer() const {
  return data_;
}

If we use it to make BlaBla and get std::vector<uint8_t> from that and that all.

std::vector<uint8_t> vec = BlaBla().toBuffer(); // <= copy here

Will be better to use ref-qualifier to move data from sigle-used temp object:

const std::vector<uint8_t> &BlaBla::toBuffer() const & { // <= method for case of usual object
    return data_;
  }
std::vector<uint8_t> BlaBla::toBuffer() const && { // <= method for case of temp object
  return std::move(data_);
}
std::vector<uint8_t> &BlaBla::asBuffer() { // <= method to access internal vector
  return data_;
}
const std::vector<uint8_t> &BlaBla::asBuffer() const { // <= method to RO-access internal vector
  return data_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant