• Home
  • Welcome to the Jungle
  • Elements of Modern C++ Style
  • About

Sutter’s Mill

Herb Sutter on software, hardware, and concurrency

Feeds:
Posts
Comments
« GotW #100: Compilation Firewalls
GotW #101: Compilation Firewalls, Part 2 (Difficulty: 8/10) »

GotW #100: Solution

2011-11-16 by Herb Sutter

image_thumb[3]

The solution to GotW #100 is now live.

Posted in C++, GotW | 8 Comments

8 Responses

  1. on 2011-11-16 at 11:44 am Mike McCarty

    “The chief drawbacks are that it requires an extra wrapper function call and normally makes the visible class useless for inheritance.”

    If you’re invoking the pimpl idiom, chances are the class was useless for inheritance before you even started.

    Not a criticism, just a statement of the (perceived) fact.


  2. on 2011-11-16 at 12:10 pm Herb Sutter

    @Mike: Not necessarily. Nearly every base class has private methods and state. If for a transitively popular base class those private parts are still changing (e.g., during the development cycle), or for any base class the base size/layout must be able to change in the future without more than a relink (e.g., to avoid the fragile base class problem), Pimpl makes lots of sense.


  3. on 2011-11-23 at 4:04 am Toby Speight

    Any comment on the advantages or otherwise of declaring a const pointer to pimpl (i.e. “const unique_ptr pimpl;”)?

    AIUI, this means that the compiler can detect accidental assignment to the pointer. It’s obviously not appropriate if you want a move constructor, I guess. Anything else to consider for or against making it const?


  4. on 2011-11-23 at 6:05 pm C++ Spring: GoingNative, Feb 2-3, 2012 « Sutter’s Mill

    [...] This is Microsoft’s first native-code-only developer event in years, and it’s not limited to Microsoft products or technologies – it’s about ISO C++ on all platforms. We’re taking the initiative to put on this event because we know that there’s a huge demand for information about the new ISO C++11 standard, but that information is still really hard to come by – the standard was just published last month, none of the major books have been updated yet to reflect it, and high-quality public information is just starting to trickle out (I’m trying to do my part too). [...]


  5. on 2011-11-28 at 3:09 pm Ben Coates

    Why not just make the unique_ptr public and implement the internal functions as anonymous namespace free functions in widget.cpp that take a widget* this pointer when called by the public functions in widget?

    Would this not clear up the back-pointer situation and reduce

    pimpl->func(this, params)
    to
    func(this, params)

    Eliminating a potential error of passing the wrong this/pimpl pair (pimpl->add(rhs, this) doesn’t look that wrong and will compile).

    This reduces pimpl entirely to an RAII version of the C handle idiom: a pointer to a incomplete type with a set of public functions in header files with external linkage, and a set of implementation details with internal linkage. It’s a mainstay of OO APIs in C.

    You’d have to revert to the more complicated version if you wanted to have protected data members, but those are discouraged in most code styles anyway.


  6. on 2011-11-29 at 10:09 am Crazy Eddie

    I have used a variation of the pimpl theme for base classes. Basically, similar to how you might implement inheritence in C, I created public and private headers of the same class. The public has the class definition, with it’s pointer to a pimpl. The private header contains a declaration of that pimpl. The public class then becomes little more than a wrapper and to inherit you actually only need to create new pimpl derivatives and possibly some overrides for creation.

    I would admit though that this has limited uses, but it’s served some important niche cases for me.


  7. on 2011-11-30 at 3:27 am Neil

    Under the solution for question 2:

    “You still need to write the visible class’ destructor yourself and define it out of line in the implementation class”

    I believe you mean “implementation file” here.


  8. on 2011-12-02 at 8:02 pm Eric

    Where you have,
    “However, it does mean adding an extra parameter to each hidden function.”
    did you mean to say something like,
    “However, it does mean adding an extra parameter to each hidden function that needs to access the visible class.”?

    As stated now, “each hidden function” seems to imply every hidden function, but it seems an advantage worth making explicit that this second approach flexibly allows using it only with those hidden functions that actually need the special access.

    About the widget constructor’s initializer list, I’m not used to seeing the curly braces, especially since it doesn’t involve an aggregate/list. Would simple parens still work? If so, is the use of curly braces just a stylistic preference or is there an advantage/reason to preferring them? Other things being equal, I would guess that use of simple parens would give more portable code that is acceptable to more compilers, but perhaps there is a compensating benefit to the curly braces.

    Thanks.



Comments are closed.

  • Tweets

    • GotW #5: Overriding Virtual Functions: Virtual functions are a pretty basic feature, but they occasionally har... bit.ly/14oTLHx 8 hours ago
    • GotW #4: Class Mechanics: How good are you at the details of writing classes? This item focuses not only on bl... bit.ly/16Fqug8 8 hours ago
    • GotW #4: Class Mechanics (7/10): How good are you at the details of writing classes? This item focuses not onl... bit.ly/10TmyVQ 4 days ago
    Follow @herbsutter
  • Popular

    • GotW #4 Solution: Class Mechanics
    • GotW #5: Overriding Virtual Functions
    • GotW #3 Solution: Using the Standard Library (or, Temporaries Revisited)
  • Categories

    • Apple
    • C# / .NET
    • C++
    • Cloud
    • Concurrency
    • Effective Concurrency
    • Friday Thoughts
    • GotW
    • Hardware
    • Java
    • Microsoft
    • Opinion & Editorial
    • Reader Q&A
    • Software Development
    • Talks & Events
    • Uncategorized
    • Web

Blog at WordPress.com.

Theme: Customized MistyLook by WPThemes.


Follow

Get every new post delivered to your Inbox.

Join 1,393 other followers

Powered by WordPress.com