title: Writing a library with C and C++ interfaces, which way to wrap?
body: <p>When preparing a library (let's call it libfoo), I find myself presented with the following dilemma: do I write it as a C++ library with a C wrapper:</p>
  
  <pre><code>namespace Foo {
    class Bar {
      ...
    };
  }
  
  /* Separate C header. #ifdef __cplusplus omitted for brevity. */
  extern "C" {
    typedef void *FooBar;
    FooBar* foo_bar_new() { return new Foo::Bar; }
    void foo_bar_delete(FooBar *bar) { delete bar; }
  }
  </code></pre>
  
  <p>Or is it better to write it as a C library with a C++ wrapper:</p>
  
  <pre><code>/* foo/bar.h. Again, #ifdef __cplusplus stuff omitted. */
  
  typedef struct {
    /* ... */
  } FooBar;
  
  void foo_bar_init(FooBar *self) { /* ... */ }
  void foo_bar_deinit(FooBar *self) { /* ... */ }
  
  /* foo/bar.hpp */
  
  namespace Foo {
    class Bar {
      /* ... */
      FooBar self;
    }
  
    Bar::Bar() {
      foo_bar_init(&amp;self);
    }
  
    Bar::~Bar() {
      foo_bar_deinit(&amp;self);
    }
  }
  </code></pre>
  
  <p>Which do you prefer, and why? I favour the latter because it means I don't have to worry about my C functions accidentally having exceptions bubble up, plus I prefer C as a language as I feel that it's a smaller semantic minefield. What do other people think?</p>
  
  <p>EDIT: So many good answers. Thanks all. It's a shame that I can only accept one.</p>