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_;
};
boost::shared_ptrdoesn’t require a complete definition other than atthe point of instantiation—in the constructor, in the case of a
pimpl.
boost::shared_ptris 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_ptrwould be more appropirate (but itdoes 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.