Description
Proposal to improve class declaration and implementation syntax
class implementation details leak into declaration
Any private member variables and methods of a class implementation have to be expolicitly declared in the class declaration (header), leading to implementation details leaking into the (public) class interface. This causes a cognitive overload of irrelevant clutter to class users, causes dependent code to require recompilation, and causes the (public) interface to mutate more frequently then necessary when changes to the implementation are made.
Proposal: a new syntax allows the class declaration (header) to remain free of implementation details (private member variables and methods) leaking into the public/published interface.
Implementation: This proposal could be implemented by synthetically allocating and generating a PImpl (pointer to implementation) in every class declaration, and causing all private member variables and methods defined in the class implementation (cpp), but not declared in the class declaration (.hpp), to become member of this automatically generated PImpl struct/class. Although this proposal will lead to every class incurring the overhead/cost of an additional pointer, this could be made optional, controlled by a (pre-)compiler option and/or a class declaration keyword to indicate the desired (implicit) use of the PImpl idiom.
C++ class implementation source is fragmented
The current syntax for implementating class methods leads to fragmented code with a large amount of repetitive "noise" with each method having to declare the class for which the method is implemented void my_class::my_method()
. The encapsulation that OOP provides is not carried over into the source code.
Proposal: class implementation syntax encapsulates the implementation source within a class implementation block, and removes the need for each method to explicitly declare which class it belongs to, e.g.
//my_class.cpp`
class my_class impl {
void my_method() {
//...
}
}
Adopting above suggestions will lead to...
- Cleaner (more readable) class declaration syntax that does not contain leaked implementation details or explicit Pimpl forward declaration
- Better separation between class/interface declaration and implementation
- More stable class declaration causing less need for recompilation when implementation changes are made
- Cleaner (more readable) class implementation syntax
- Better encapsulation (less fragmentation) of class implementation source
Example of proposed CPP2 syntax
//Declaration: my_class.hpp`
class my_class {
public:
void my_method();
private:
int my_var;
// CPP2: Automatic declaration of my_class::PImpl (forward class and pointer)
// CPP2: no need to declare private implementation member variables and functions in header
}
//Implementation: my_class.cpp`
class my_class impl { // impl keyword provides context for member variables and functions
void my_method() { // my_class:: prefix not required
// implementation
}
private:
// private (undeclared) member variable used by implementation
int var_impl {0}; // CPP2: implicitly declared in my_class::PImpl
// private (undeclared) member function used by implementation
void method_impl() { //CPP2: implicitly declared in my_class::PImpl
// implementation
}
}