Medium
PImpl with a compiler-generated destructor
Widget.h declares ~Widget() implicitly (i.e. does not declare it at all, letting the compiler generate it). What happens when a translation unit that only includes Widget.h (not Widget.cpp) constructs and destroys a Widget?
Widget.h
// Widget.h
#include <memory>
class Widget {
public:
Widget();
void doWork();
// no destructor declared -- relying on the implicit one
private:
struct Impl;
std::unique_ptr<Impl> pImpl_;
};
// Widget.cpp
#include "Widget.h"
struct Widget::Impl { int state = 0; };
Widget::Widget() : pImpl_(std::make_unique<Impl>()) {}
void Widget::doWork() { pImpl_->state++; }
// user.cpp
#include "Widget.h"
int main() {
Widget w;
w.doWork();
} // w destroyed here11 of 44 in Design Patterns