Search code examples
c++c++11unique-ptrlibevent

Libevent with C++11+ unique_ptr


I've recently tried to wrap libdbus types and libevent types with std::unique_ptr and custom deleter to simplify the code but I have a error with these libraries:

/opt/cross/armv7hl-meego-linux-gnueabi/include/c++/4.8.3/bits/unique_ptr.h:65:22: error: invalid application of 'sizeof' to incomplete type 'sockets::libev::event_base'
  static_assert(sizeof(_Tp)>0,
                      ^

The code is simple:

namespace sockets {
    // ... unix sockets stuff
    namespace libev {
        #include <event.h>
    } // libev
} // sockets

class A {
public:
    void run() {
        using namespace sockets;
        using namespace sockets::libev;
        using UniqueEventBase = std::unique_ptr<event_base>;
        // ...
    }   
};

So how do I write RAII-wrappers for event_base struct in this example ?

P.S. I have found that event_base struct is forward declared in event2/event.h. So there is no option to wrap it?


Solution

  • Since std::unique_ptr does not type-erase its deleter, and the default deleter does not work on incomplete types, you need to provide an alternative deleter as part of the type signature:

    struct EventBaseDeleter {
      void operator()(event_base* ptr) const {
        sockets::libev::event_base_free(ptr);
      }
    };
    
    using UniqueEventBase = std::unique_ptr<event_base, EventBaseDeleter>;