[optional] References

Greetings, boost::optional<T &> seems to be implemented in terms of a boolean flag and an aligned_storage. Wouldn’t it make more sense to make boost::optional<T &> a thin wrapper over T*? This would save a couple of bytes, speed up get_ptr by a couple of ticks, and perhaps reduce compilation times. Also, from the documentation[1]:
Rebinding semantics for the assignment of initialized optional references has been chosen to provide consistency among initialization states even at the expense of lack of consistency with the semantics of bare C++ references. It is true that optional<U> strives to behave as much as possible as U does whenever it is initialized; but in the case when U is T&, doing so would result in inconsistent behavior w.r.t to the lvalue initialization state.
optional<U> does not have the semantics of U, but rather those of U*. That explains the assignment semantics perfectly well: int a = 1; int b = 2; int& ra = a; int& rb = b; int* pa = &a; int* pb = &b; boost::optional<int &> oa(ra); boost::optional<int &> ob(rb); ra = rb; // assigns b to a pa = pb; // rebinds pa to b oa = ob; // rebinds oa to b -- [1] http://tinyurl.com/55s8f7 -- WBR Roman.
participants (1)
-
Roman Odaisky