PDA

View Full Version : When to use pimpl ?



Gopala Krishna
28th July 2007, 12:00
My question is simple. When exactly pimpl is supposed to be used ? I know it reduces compile time but slightly increases the runtime due to double indirection.
Using it in library is a big plus. But sometimes i get doubt whether it should be used in *application* or not ?
Any thoughts ?

jacek
28th July 2007, 15:47
The main advantage is that it helps to preserve backward binary compatibility of libraries, but this isn't very helpful for applications. So I would say that you can use it in an application, if:
you want to implement data sharing (although pimpl goes a bit further than that),
you want to reduce the compilation time (especially if you don't want to recompile half of your application only because you moved some part of the code to a private method),
or, finally, you expect that a part of your code might be turned into a library and you don't want to code things twice.

pdima
26th August 2007, 13:45
The main advantage is that it helps to preserve backward binary compatibility of libraries, but this isn't very helpful for applications. So I would say that you can use it in an application, if:
you want to implement data sharing (although pimpl goes a bit further than that),
you want to reduce the compilation time (especially if you don't want to recompile half of your application only because you moved some part of the code to a private method),
or, finally, you expect that a part of your code might be turned into a library and you don't want to code things twice.

I would also add that usage of pimpl allows to hide all ( or at least most of ) the implementation details from the header and makes reading of headers easier.

Kumosan
27th August 2007, 10:36
I would also add that usage of pimpl allows to hide all ( or at least most of ) the implementation details from the header and makes reading of headers easier.

I makes reading the header easier. But if you need to read and understand 3rd-party code, it makes things (IMHO) a bit more difficult.

wysota
27th August 2007, 14:43
But if you need to read and understand 3rd-party code, it makes things (IMHO) a bit more difficult.

This is true, but the question is when it should be used. So in that case my answer is "when you want to make sure that even if you get fired, they'll have to hire you again to maintain the code you wrote, because nobody else understands it". It's called a back door ;)

Gopala Krishna
27th August 2007, 17:15
Thanks for answering my question.
I just discovered one more advantage of using pimpl though (may sound silly ;) )
By adopting pimpl you don't need that ugly "m_" prefix(variations of this form too) to member variables :D

marcel
27th August 2007, 22:00
By adopting pimpl you don't need that ugly "m_" prefix(variations of this form too) to member variables :D
Well, you don't have to do that. It is not a rule, or anything.
It not confusing if you don't apply it, especially if you don't use static variables.

wysota
27th August 2007, 22:19
You just can't use getters that are named after the variables (you have to use a prefix in the getter like getMember() instead of member()).

Gopala Krishna
28th August 2007, 14:27
You just can't use getters that are named after the variables (you have to use a prefix in the getter like getMember() instead of member()).

Exactly! :)
BTW i assume the runtime penalty of using pimpl is not very significant since i'll start usage of pimpl in my project soon.

wysota
7th September 2007, 11:15
Just don't use pimpl only because it exists.

sunil.thaha
7th September 2007, 11:39
Just don't use pimpl only because it exists.

What is the suitable candidate for pimpl.

Ofcourse, I know of the Containers :) and how pimpl helps in maintaining the binary compatiblity. What else are the candidates .

And when exactly should we not use pimpls.

wysota
7th September 2007, 13:27
And when exactly should we not use pimpls.

When it doesn't help you in any way.

sunil.thaha
7th September 2007, 13:48
Can you be a bit more elaborate please?

wysota
7th September 2007, 14:22
Hmm... When you don't clearly see any advantages of using p-impl in a particular situation then don't use it there. Simple as that.

sunil.thaha
7th September 2007, 15:04
Wow !!
Thanks a lot !!


BTW ?
Were you the one who expanded


( a + b )^2 as ( a + b ) ^2 :p

wysota
7th September 2007, 15:14
Well... what more can I say here. A general rule is that if you don't see a need for something, don't use it (unless you think a need could arise later).

Gopala Krishna
7th September 2007, 15:33
Just don't use pimpl only because it exists.

Yeah true. I bought exceptional c++ which explains clearly on how and when to use pimpl :)

Prakash Nadar
17th July 2009, 00:14
If an object is created many times, i.e. list-items or object on the list in the order of 100s or more, the pimpl implementation will have atleast 2 objects created / object and hence can lead to memory fragmentation.

Another advantage of pimpl that i can see is porting your code to different platforms. i.e. Since we use QT and if we use certain part of the OS to do specific thing that QT does not have the class or API for. Then the private implementation can be implemented specific to each platform and then #def it as per the platform.