mercredi 28 septembre 2016

vector of unique_ptr, inheritance?

Assuming this code:

class Parent {}
class Child : public Parent {}

static std::vector<std::unique_ptr<Child>> Foo();

Is there a simpler way to write this function:

std::vector<std::unique_ptr<Parent>> Bar() {
  auto children = Foo();

  std::vector<std::unique_ptr<Parent>> parents;
  result.insert(result.end(), std::make_move_iterator(children.begin()),
                std::make_move_iterator(children.end()));
  return parents;
}

This doesn't work:

std::vector<std::unique_ptr<Parent>> Bar() {
  return Foo(); // Compiler error: cannot convert from vector<...> to vector<...>
}

Aucun commentaire:

Enregistrer un commentaire