C++ Logo


Advanced search

Re: Partial type definition

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Thu, 26 Aug 2021 14:49:04 -0400
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
  std::unique_ptr<impl> impl_;

  iface(params); //No definition.
  ~iface(); //No definition.

  iface(iface const&); //No definition.


As long as you don't *define* the member functions in the header
(which your idea doesn't support anyway), this code is fine.

Received on 2021-08-26 13:49:17