
On Saturday, November 19, 2011 18:26:25 Andrzej Krzemienski wrote:
Interesting. It looks like optional can be seen as a special container with the maximum size of 1, and then functions like clear() or empty() come as natural. On the other hand it can be seen as a pointer-like entity with value semantics, and then functions like reset() and literal nullptr come as natural.
Ok, I have no strong preference between reset() or clear() for making the optional empty. However, a explicit empty() member would be useful anyway. class MyClass { optional< int > m_x; public: bool has_x() const { return !m_x.empty(); } }; With the current interface I should have written has_x() in a more obscure way, like !!m_x or m_x.get_ptr() != NULL.
I withdraw my suggestion to use nullptr. Function reset() would be a better choice. I also find reset() preferable to clear() because reset can be extended to accept parameters and the name does not become confusing; similarly to unique_ptr::reset().
I think, providing the reset() member with construction semantic is not a good idea because it would make imposible in-place default construction. There should be another method that would handle construction. In another post I suggested assign() but I'm happy if someone comes up with a better name.