C++

C++ - Pimpl Idiom

Posted by Rico's Nerd Cluster on March 18, 2023

Minimal Structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// header

// Widget.hpp
#pragma once
#include <memory>

class Widget {
public:
  Widget();
  ~Widget();

  void doSomething();

  // (Rule of five if you need copy/move; omitted for brevity)

private:
  struct Impl;
  std::unique_ptr<Impl> pImpl;
};

In source file, we can have all the implementation in here to reduce recompilation-time upon changes (since this is a compilation unit and its code is compiled once per change).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Widget.cpp
#include "Widget.hpp"
#include <iostream>

// 1) Define the Impl struct in the .cpp so its details stay hidden:
struct Widget::Impl {
  void doSomethingImpl() {
    std::cout << "Widget::Impl doing work\n";
  }
};

// 2) Forward all Widget methods to Impl:
Widget::Widget() : pImpl(std::make_unique<Impl>()) {}
Widget::~Widget() = default;

void Widget::doSomething() {
  pImpl->doSomethingImpl();
}

Is pimpl worth it??

I forgot the return keywrod. Then I was stuck at the assocaited abort for 1h+. So please be aware that pimpl boiler plate could be a source of bugs

```cpp NavState IEKFLIO::IEKFLIO::get_current_state() const { impl_->get_current_state(); } `