C++ Logo

std-proposals

Advanced search

Partial type definition

From: Valery Osheter <valery.o_at_[hidden]>
Date: Sun, 22 Aug 2021 15:27:12 +0300
The compiler allows the forward declaration of a type name only. The type
name then can be used as a reference or a pointer only. If we want to call
a method of this type, a complete type definition is required. I understand
that this requirement comes from the storage details. But this compiler's
restriction is unnecessery for static and non-virtual methods, and other
things that are not related to the instance data.

A programmer just wants to tell the compiler that the class/struct will
have a method named X with parameters Y. Why is it problematic to the
compiler? The same is about class constants and enums.
A partial definition of type methods, constants and enums improves the
readability of the code, hides implementation details, and speeds up
compilation.

Hiding implementation details in C++ is done today either with PIMPL or
interface definition. Interfaces are closest to the partial type
definition, but still their main purpose is an abstract interface
implemented by several different classes. If there is only one class, then
placing its public methods into the interface is an unnecessary
overcomplication. I propose a partial class definition that simplifies and
allows to have only one type name.

The 'extern' declaration of class only without variable name can be used
for partial type definition:

// .h file
extern struct worker
{
  void work();
};

inline void do_work(worker& w)
{
  w.work();
}

// .cpp file
struct worker
{
  void work(); // optional declaration
private:
  int x=0;
};

void worker::work()
{
  x++;
}

Received on 2021-08-22 07:27:25