Skip to content

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

Open

Description

@xDimon

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions