How about this solution:
template<typename T> struct foo { T* p; foo(T* x) : p(x) {} ~foo() { if(p) delete p; } template<typename U> struct impl { U& deref(U* p) { return *p; } }; template<> struct impl<void> { void deref(void* p) { } }; typename boost::conditional<std::is_void<T>::value, T, typename std::add_reference<T>::type>::type operator*() const { static_assert(!std::is_void<T>::value, "illegal use of type 'void'"); return impl<T>().deref(p); } };
source share