Boost logo

Boost :

From: ebf_at_[hidden]
Date: 2002-07-28 23:42:50

I have developed an efficient (i.e. stack-based) encapsulation of Herb
Sutter's pimpl idiom that satisfies the strong exception-safety

For those aware of the typical shortcomings of stack-based pimpl
solutions, my implementation overcomes the shortcomings of the
rudimentary solution Mr. Sutter proposes (and knocks down) on pages 117-
118 of _Exceptional C++_.

Example use of the library is as follows:

// x.h
#include "boost/stack_pimpl.hpp"

class X {
  struct Impl;
  static const size_t sizeofimpl = /* value >= sizeof(Impl) */;
  stack_pimpl<Impl, sizeofimpl> pimpl_;

  explicit X(int);
  void swap(X&);

  int value() const;

// x.cpp
#include "x.h"

struct X::Impl {
  int i;

X::X(int value) {
  pimpl_.get().i = value;

X::~X() {
  // stack_pimpl self-destroys, but ~X must be not be implicitly defined

void X::swap(X& operand) {

int X::value() {
  return pimpl_.get().i;

--end code example--

The current version of the library is available at:

Let me know what you think.

Eric Friedman

Boost list run by bdawes at, gregod at, cpdaniel at, john at