dimanche 21 juin 2020

Error of returning rvalue of std::unique_ptr?

Error case (why does the destructor of class A will be called by twice?):

class A {};
std::unique_ptr<A>&& get() {
  std::unique_ptr<A> p(new A());
  return std::move(p);
}
int main() {
  std::unique_ptr<A> k = get();
}

Normal Case:

class A {};
std::unique_ptr<A> get() {
  std::unique_ptr<A> p(new A());
  return std::move(p);
}
int main() {
  std::unique_ptr<A> k = std::move(get());
}

Aucun commentaire:

Enregistrer un commentaire