So I've been thinking about PIMPL and stack allocation. I've been writing a library and decided to use PIMPL to hide the private member of the class. That means I would have a class declared like this
class Foo {
private:
class Handle;
std::tr1::shared_ptr<Handle> handle;
public:
Foo();
};
It's pretty straight forward. But then in the constructor you do this
Foo::Foo() : handle(new Handle()) {}
So when someone using my library creates a Foo on the stack, they are essentially doing a heap allocation anyway. Is this the trade-off you have to live with when using PIMPL? I thought of releasing the documentation with a warning next to constructors: "WARNING: This results in a heap allocation" or somesuch.
My other thought was to have all classes which are exposed to the implementation as pure virtual interfaces and a whole bunch of static factory methods returning smart pointers. This also means heap allocation but there is no trick to it.
Any thoughts or suggestions? Am I being overly-considerate of programmers using my library?