samedi 28 septembre 2019

What is the correct way to call a move constructors on the derived class?

I'm writing a piece of code that inherits from a user provided class (not for polymorphism reasons). I need to write an explicit move constructor/assignment for it because I store pointers to some internal data of my class. I don't know how to call move constructor well.

Example:

template <typename UserType>
class my_class : UserType  {
  other_data data;
 public:
  my_class(my_class&& x) UserType(/*...*/), data(std::move(x.data)) {}
};

Options:

  • I can call move(x) => but then it's use after move.
  • I can do some static_cast<UserType&&>(x) - but that's quite unusual.

What's a good solution here?

Aucun commentaire:

Enregistrer un commentaire