
Am Tuesday 27 October 2009 15:13:21 schrieb Scott McMurray:
struct optional_error_code_ref { optional_error_code_ref() : p_() {} optional_error_code_ref(optional_error_code_ref const &ecr) : p_(ecr.p_) {} optional_error_code_ref(error_code &r) : p_(&r) {} error_code &operator*() const { return *p_; } error_code *operator->() const { return p_; } operator safe_bool() const { return safe_bool(p_); } private: error_code *p_; };
It would of course be better spelt qux<error_code&>, but I'm not sure exactly what name that should be.
boost::optional<error_code&>? optional does have implicit conversion and the rest of your public interface. optional<T &> is implemented as a container of T & plus a bool though, but I see no reason not to create a boost::optional specialization that implements optional<T &> by holding a pointer that's exposed as a reference in the interface.