std::unique_ptr and std::shared_ptr as parameters for virtual functions

Viewed 513

I'm designing a C++ class interface based on virtual methods to be able to provide extensibility points.

Lots of these public methods require heap allocated object as parameters.

Since I'm making use of modern C++ patterns I'm planning to use std::unique_ptr or std::shared_ptr for that but I have doubts on both of them.

Using std::unique_ptr it looks like something like this:

class IFoo {
  virtual void doSomethingWithUser(std::unique_ptr<User> user) = 0;
}

Forcing the caller to provide std::unique_ptr has downsides:

  • the caller cannot do any operation on the provided user since it has to be moved
  • in case any of doSomethingWithUser implementation needs to store the user in some container, it is not constructible from std::shared_ptr

Using std::shared_ptr for all the public methods could solve the problem but we have to pay for extra memory space plus the atomic increment and decrement of the references count.

Is there any rule of thumb I can follow?

1 Answers
Related