CKTIS / C++
ABI and PImpl: hiding implementation without promising full binary compatibility
PImpl moves a class's data into a hidden implementation object. It reduces header dependencies and helps preserve the public class layout, but does not guarantee ABI compatibility by itself. The key mechanics are an incomplete nested type and an out-of-line destructor.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
class Widget { struct Impl; std::unique_ptr<Impl> impl_; public: Widget(); ~Widget(); }; // Define Impl, constructor, and destructor in the implementation file.An ABI (Application Binary Interface) is the set of rules by which compiled components interact, including object layout, symbol naming, and calling conventions. PImpl (pointer to implementation) hides data behind a pointer to a separate object. Changing fields in Impl does not add fields to Widget, helping preserve a binary boundary and reduce header dependencies. This is not a portable compatibility guarantee across compilers, standard libraries, or build settings.
Inside Widget, struct Impl; declares an incomplete nested type. A header can contain std::unique_ptr<Impl> without defining Impl, but its default deleter requires a complete type when deleting the object. Declare ~Widget(); in the header and define Widget::~Widget() = default; after the definition of Impl in the implementation file: this is an out-of-line destructor. The constructor is also placed where the type is complete.
extern "C" specifies C language linkage for applicable function and variable names and function types; it neither hides data nor gives a class a C-compatible layout. It is not a substitute for PImpl or a universal ABI-compatibility switch. A boundary intended for C also needs appropriate parameter types, ownership rules, and error handling; C++ exceptions should not cross that boundary.
The example combines header, implementation, and client sections in one file. Split them in a library so clients cannot see Impl. Copying is explicitly disabled, and moving is not provided; if you add move operations, define them out of line after Impl and specify how moved-from objects may be used. Costs include allocation and an extra indirection. The example uses the C++23 flag; production runtime behavior is not attested. The rules are sourced from an evolving working draft, not a frozen C++23 edition.
Examples
#include <iostream>
#include <memory>
// Public header section
class Widget {
public:
Widget();
~Widget();
Widget(const Widget&) = delete;
Widget& operator=(const Widget&) = delete;
int value() const;
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
// Implementation section
struct Widget::Impl {
int value = 42;
};
Widget::Widget() : impl_(std::make_unique<Impl>()) {}
Widget::~Widget() = default;
int Widget::value() const { return impl_->value; }
int main() {
Widget widget;
std::cout << widget.value() << '\n';
}
Result
42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.