
Thorsten Ottosen wrote in his OP:
I have some comments on the way optional<T>::swap()
Hmmm... I can't find optional<T>::swap in my copy of optional.hpp (straight from the trunk)! It looks like optional<T> only has a non-member boost::swap function! I think it should definitely have a swap member function, as well! As a guideline, whenever providing a non-member swap function for a class type, one should provide a swap member function as well! (E.g., Scott Meyers, Effective C++ Third Edition, item 25!). Why? Because the STL does it that way, /and/ because it allows swapping a temporary. Of course, optional<T>::swap could be implemented very easily: void swap( optional & arg ) { using std::swap; swap(*this, arg); } By default, it would call the original boost::swap(optional<T>&, optional<T>&), by appying ADL. But it would also allow end users to customize the behavior of optional<T>::swap for their specific types, either by providing a template specialization of boost::swap or std::swap, or by adding an overloaded swap function to the namespace of T. Thorsten Ottosen wrote:
I can't imagine that it would result in lower performance in general ... have you seen many types for which default construction was more expensive than copy-construction?
Clearly if T has a nothrow default-constructor, it would make sense to use it, when swapping optional<T>. But otherwise it's hard to say "in general" what swap implementation is best for any optional<T>. In that case the user could still provide her own custom swap function for optional<T>, for her particular type T, as I just described. Wouldn't that solve the issue? Kind regards, Niels