samedi 5 mai 2018

Returning unique_ptr

I have a method like this:

std::unique_ptr<const Stats> Table::GetStats() const {
  unique_ptr<Stats> result;
  // ...
  // Prepare stats. Under some conditions an exception may be thrown.
  // ...
  return result;
}

The problem is that it doesn't compile:

error: cannot bind ‘std::unique_ptr’ lvalue to ‘std::unique_ptr&&’

I can make it compile by using the following bypass:

return unique_ptr<const Stats>(result.release());

But it seems a bit as doing something excessive. I cannot understand, what's wrong with the first piece of code from C++'s point of view? Is there more elegant solution?

Aucun commentaire:

Enregistrer un commentaire