C++ Logo

std-proposals

Advanced search

Re: Partial type definition

From: Valery Osheter <valery.o_at_[hidden]>
Date: Thu, 26 Aug 2021 22:28:58 +0300
The advantages of PIMPL are:
- useful instance creation
- general flexibility
The disadvantages of PIMPL are:
- overhead of the indirect access
- requires a wrapper for each method

The advantages of forward declaration
- direct method call
- requires no PIMPL boilerplate, no wrapper but declaration
The disadvantages of forward declaration
- the instance cannot be created without full class definition
- virtual functions are unavailable


On Thu, Aug 26, 2021, 21:49 Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Thu, Aug 26, 2021 at 2:24 PM Valery Osheter via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > In case of PIMPL you have to wrap each interface method. My proposal
> requires only to declare methods. Does it not make the forward method
> declaration attractive to people?
> > It does not allow to create the instance of the class as PIMPL does,
>
> That's my point. Being able to create an instance of the PIMPL class
> is kind of important for many existing uses of PIMPL.
>
> > but except that it allows to write all tests of the class and to do it
> directly without a PIMPL intermediary.
>
> How does this affect the point I just made?
>
> Also, it's not clear to me why it matters if your tests can be written
> against forward declarations. Even if the person writing the tests and
> the person implementing the type are two different people, they can be
> working off of the same header file, right? So how does being able to
> forward declare members help with writing tests?
>
> > Note that the implementation class must be defined at the place PIMPL
> creates its instance. This is the same as in my method, the object creation
> requires the class to be defined anyway.
>
> No, it does not. The implementation class needs to be *declared*
> there, but it need not be *defined* there:
>
> ```
> class impl; //declaration, not definition.
>
> class iface
> {
> private:
> std::unique_ptr<impl> impl_;
>
> public:
> iface(params); //No definition.
> ~iface(); //No definition.
>
> iface(iface const&); //No definition.
>
> //etc
> };
> ```
>
> As long as you don't *define* the member functions in the header
> (which your idea doesn't support anyway), this code is fine.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2021-08-26 14:29:11