Is the pImpl idiom really used in practice?

I am reading the book "Exceptional C++" by Herb Sutter, and in that book I have learned about the pImpl idiom. Basically, the idea is to create a structure for the private objects of a class and dynamically allocate them to decrease the compilation time (and also hide the private implementations in a better manner).

For example:

class X
{
private:
  C c;
  D d;  
} ;

could be changed to:

class X
{
private:
  struct XImpl;
  XImpl* pImpl;       
};

and, in the CPP, the definition:

struct X::XImpl
{
  C c;
  D d;
};

This seems pretty interesting, but I have never seen this kind of approach before, neither in the companies I have worked, nor in open source projects that I've seen the source code. So, I am wondering it this technique is really used in practice?

Should I use it everywhere, or with caution? And is this technique recommended to be used in embedded systems (where the performance is very important)?


So, I am wondering it this technique is really used in practice? Should I use it everywhere, or with caution?

Of course it is used, and in my project, in almost every class, for several reasons you mentioned :

  • data hiding
  • recompilation time is really decreased, since only the source file needs to be rebuilt, but not the header, and every file that includes it
  • binary compatibility. Since the class declaration doesn't change, it is safe to just update the library (assuming you are creating a library)
  • is this technique recommended to be used in embedded systems (where the performance is very important)?

    That depends on how powerful your target is. However the only answer to this question is : measure and evaluate what you gain and lose.


    It seems that a lot of libraries out there use it to stay stable in their API, at least for some versions.

    But as for all things, you should never use anything everywhere without caution. Always think before using it. Evaluate what advantages it gives you, and if they are worth the price you pay.

    The advantages it may give you are:

  • helps in keeping binary compatibility of shared libraries
  • hiding certain internal details
  • decreasing recompilation cycles
  • Those may or may not be real advantages to you. Like for me, I don't care about a few minutes recompilation time. End users usually also don't, as they always compile it once and from the beginning.

    Possible disadvantages are (also here, depending on the implementation and whether they are real disadvantages for you):

  • Increase in memory usage due to more allocations than with the naïve variant
  • increased maintenance effort (you have to write at least the forwarding functions)
  • performance loss (the compiler may not be able to inline stuff as it is with a naïve implementation of your class)
  • So carefully give everything a value, and evaluate it for yourself. For me, it almost always turns out that using the pimpl idiom is not worth the effort. There is only one case where I personally use it (or at least something similar):

    My C++ wrapper for the linux stat call. Here the struct from the C header may be different, depending on what #defines are set. And since my wrapper header can't control all of them, I only #include <sys/stat.h> in my .cxx file and avoid these problems.


    Agree with all the others about the goods, but let me put in evidence a limit: doesn't work well with templates .

    The reason is that template instantiation requires the full declaration available where the instantiation took place. (And that's the main reason you don't see template methods defined into CPP files)

    You can still refer to templetised subclasses, but since you have to include them all, every advantage of "implementation decoupling" on compiling (avoiding to include all platoform specific code everywhere, shortening compilation) is lost.

    Is a good paradigm for classic OOP (inheritance based) but not for generic programming (specialization based).

    链接地址: http://www.djcxy.com/p/40252.html

    上一篇: 允许为零

    下一篇: pImpl习语在实践中是否真的被使用?