Search code examples
c++templatesboostpimpl-idiomscoped-ptr

pimpl-idiom in template; which smart pointer?


I usually use a boost::scoped_ptr for pimpl's (for one reason because then I don't get surprises if I forget to deal with the copy constructor)

With templates however I can't just put the destructor in the cpp file where the impl is fully defined in order to fulfill the requirements of scoped_ptr's destructor. It does work anyway but I'm not sure if its garanteed to work or just by chance. Is there some 'best practice' or standard? Is scoped_ptr the best smart pointer for pimpls in non-copyable classes?

template <class T> class C {
public:
    C(){}
    ~C(){}
private:
    boost::scoped_ptr<T> pimpl_;
};

Solution

  • boost::shared_ptr doesn't require a complete definition other than at the point of instantiation—in the constructor, in the case of a pimpl. boost::shared_ptr is not appropriate for the pimpl idiom, however, since it gives very unexpected semantics (reference semantics for assignment or copy); if you really want the added complexity of a smart pointer, boost::scoped_ptr would be more appropirate (but it does require a full definition at the point its destructor is instantiated).

    With regards to templates, it makes no sense to use the pimpl idiom for the implementation details from the header. In the absense of export, all of the implementation details of a class template must be included everywhere the template is used, so the motivation behind the pimpl idiom ceases to exist.