Results 1 to 18 of 18

Thread: When to use pimpl ?

  1. #1
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default When to use pimpl ?

    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 ?
    Last edited by Gopala Krishna; 28th July 2007 at 12:19. Reason: Corrected a wrong word
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When to use pimpl ?

    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.

  3. #3
    Join Date
    Aug 2007
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When to use pimpl ?

    Quote Originally Posted by jacek View Post
    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.

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: When to use pimpl ?

    Quote Originally Posted by pdima View Post
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When to use pimpl ?

    Quote Originally Posted by Kumosan View Post
    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

  6. #6
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: When to use pimpl ?

    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
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When to use pimpl ?

    Quote Originally Posted by Gopala Krishna View Post
    By adopting pimpl you don't need that ugly "m_" prefix(variations of this form too) to member variables
    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When to use pimpl ?

    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()).

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: When to use pimpl ?

    Quote Originally Posted by wysota View Post
    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.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When to use pimpl ?

    Just don't use pimpl only because it exists.

  11. #11
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When to use pimpl ?

    Quote Originally Posted by wysota
    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.
    We can't solve problems by using the same kind of thinking we used when we created them

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When to use pimpl ?

    Quote Originally Posted by sunil.thaha View Post
    And when exactly should we not use pimpls.
    When it doesn't help you in any way.

  13. #13
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When to use pimpl ?

    Can you be a bit more elaborate please?
    We can't solve problems by using the same kind of thinking we used when we created them

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When to use pimpl ?

    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.

  15. #15
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: When to use pimpl ?

    Wow !!
    Thanks a lot !!


    BTW ?
    Were you the one who expanded
    Qt Code:
    1. ( a + b )^2 as ( a + b ) ^2 :p
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  16. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: When to use pimpl ?

    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).

  17. #17
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: When to use pimpl ?

    Quote Originally Posted by wysota View Post
    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
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  18. #18
    Join Date
    Jul 2009
    Posts
    1
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default Re: When to use pimpl ?

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.