Since boost::optional<T&> is already a specialisation, why isn't it just implemented as a wrapper around T*? This would allow it to occupy less space, since there is no need for the m_initialized boolean.
Since boost 1.61 optional is optimized in the case of references.
The release notes mention :
sizeof(optional<T&>) == sizeof(T*)
hence it's certainly implemented as a pointer in that case.
Probably so because an uninitialized boost::optional<T*> object must be distinct from boost::optional<T*> initialized with NULL, e.g. this function can return no value, a NULL or a non-NULL pointer.
Why don't you use a plain pointer in this case with NULL indicating no value. No need to add more complexity on top of that with boost::optional<>. I mean, it is easy to make things bigger or more complex but it's hard to make them any better.
Firstly, boost::optional<T&> is not a specialization. If you look at the code, you'll see that it does do some tag-based dispatch to customize behavior for reference types, but the boost::optional_base<T> class template itself is not specialized.
However it is still a legitimate question as to why this space optimization is not implemented. Possibly because it isn't specialized, the job is that much harder, I don't know.
The question as to why you'd prefer optional<T&> over a raw pointer is a completely separate one, so feel free to ask it separately...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With