Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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_;
};
like image 353
odinthenerd Avatar asked Dec 05 '11 14:12

odinthenerd


People also ask

When to use PImpl idiom?

The PImpl Idiom (Pointer to IMPLementation) is a technique used for separating implementation from the interface. It minimizes header exposure and helps programmers to reduce build dependencies by moving the private data members in a separate class and accessing them through an opaque pointer.

What is PImpl in design pattern?

"Pointer to implementation" or "pImpl" is a C++ programming technique that removes implementation details of a class from its object representation by placing them in a separate class, accessed through an opaque pointer: // -------------------- // interface (widget.


1 Answers

It just happens that Herb Sutter started to write his GotWs again after a long time. One of the first new ones are related to "Compilation Firewalls".

You might want to take a look at:

GotW #100: Compilation Firewalls (Difficulty: 6/10)

and

GotW #101: Compilation Firewalls, Part 2 (Difficulty: 8/10)

like image 56
ds27680 Avatar answered Sep 25 '22 03:09

ds27680