yayuj
yayuj

Reputation: 2464

Best choice over boost:scoped_ptr<T> pimpl

I'm not using boost just the standard library and I was seeing in some questions that the main difference between boost::scoped_ptr<T> and std::unique_ptr is that boost::scoped_ptr<T> is neither copyable nor movable, then I was wondering, what is the best choice over boost::scoped_ptr<T>? Using std::unique_ptr or using raw pointers with the rule of three/five in order to avoid copying and moving?

Upvotes: 0

Views: 154

Answers (2)

Bryan Chen
Bryan Chen

Reputation: 46578

I suggest to use const std::unique_ptr.

You almost never want to manage raw pointers directly so unique_ptr is the only sensible option (shared_ptr isn't suitable for pimpl, rarely you want to share the impl object). Adding const will make it non-movable, like scoped_ptr.

Upvotes: 5

Richard Hodges
Richard Hodges

Reputation: 69864

The question is a little ambiguous, but the answer is very definitively that you should use either a std::unique_ptr or a std::shared_ptr to hold your pimpl, depending on whether you want your class to share state (shared_ptr) or have exclusive access with the benefit of being automatically moveable (unique_ptr).

Management of naked pointers is extremely difficult to get right, impossible if you manage more than one in the same class. unique_ptr was created to help you write flawless programs with ease.

You would be wise to use it.

Upvotes: 1

Related Questions