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
guarantee.

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_;

public:
  explicit X(int);
  ~X();
  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) {
  pimpl_.swap(operand.pimpl_);
}

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

--end code example--

The current version of the library is available at:

  http://groups.yahoo.com/group/boost/files/stack_pimpl-20020728.zip

Let me know what you think.

Eric Friedman


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk