Why should a pimpl be declared as a struct and not a class?
Asked Answered
S

3

7

The canonical form of the pimpl idiom (from Herb Sutter's "Exceptional C++") is as follows:

class X 
{
public:
 /* ... public members ... */
protected:
 /* ... protected members? ... */ 
private:
 /* ... private members? ... */
 struct XImpl;
 XImpl* pimpl_; // opaque pointer to
                // forward-declared class 
};

My question is, why is XImpl declared as a struct instead of a class?

Scenography answered 18/9, 2014 at 7:18 Comment(4)
There is little sense is hiding something that is already hidden. The default access of private for a class vs. public for a struct seems trivially overkill.Dyaus
It makes absolutely no difference whatsoever (unless your compiler is very very buggy).Juneberry
@Dyaus But that is a forward declaration. In this particular case, it makes absolutely no difference. It doesn't even save typing :-)Songwriter
@Songwriter I completely agree. its the language-difference of the word as a normal decl I was commenting on. you're absolutely right that it truly makes no functional difference at all.Dyaus
E
7

The only difference between struct and class is the default access control of the bases and members (public and private, respectively). You can even declare the same type with one and define it with the other (but note that some compilers can issue warnings on this).

Just use whatever is most natural to you. @WhozCraig correctly points out that since XImpl is already inaccessible outside of the implementation X, making its members private by default seems superfluous. Still, as I've said above, it makes no difference here at all, since it's only the keyword used for the definition which matters.

Eec answered 18/9, 2014 at 7:24 Comment(7)
But it is a forward declaration, so the difference doesn't matter here (except some might get confused seeing class in a declaration and struct in the definition.)Songwriter
@StilesCrisis Really? Since when? ? C++11 §11(3) [class.access] : "Members of a class defined with the keyword class are private by default"Dyaus
Class definitely defaults to private!Polypary
@Songwriter and some compilers even emit warnings if you define a class after forward-declaring it as a structSavell
@Songwriter Right, edited the answer to that effect.Eec
@Angew an example for the slight difference (the compiler warning mentioned above): coliru.stacked-crooked.com/a/45b934a46a183d27 This is clang specific, it does not appear under gcc.Savell
@ArneMertz IIRR, VS issues this warning as well. But I must say it's one of the few warnings I consider detrimental, and disable it in my projects.Eec
P
2

Well... I don't know why Herb Sutter has decided to use struct, but you can use class instead if you prefer, it is equivalent in this case.

Preciado answered 18/9, 2014 at 7:22 Comment(0)
A
2

It doesn't really make a difference since structs and classes are basically the same, but you probably want everything in XImpl to be public because it's only visible to the code that implements X anyway. Using struct instead of class just saves you from having to write public: at the beginning of its definition.

Airsick answered 18/9, 2014 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.