2

I found the following code on STL's implementation for optional (experimental/optional):

template <class _Up>
_LIBCPP_INLINE_VISIBILITY
constexpr value_type value_or(_Up&& __v) const&
{
    static_assert(is_copy_constructible<value_type>::value,
                  "optional<T>::value_or: T must be copy constructible");
    static_assert(is_convertible<_Up, value_type>::value,
                  "optional<T>::value_or: U must be convertible to T");
    return this->__engaged_ ? this->__val_ :
                              static_cast<value_type>(_VSTD::forward<_Up>(__v));
}

template <class _Up>
_LIBCPP_INLINE_VISIBILITY
value_type value_or(_Up&& __v) &&
{
    static_assert(is_move_constructible<value_type>::value,
                  "optional<T>::value_or: T must be move constructible");
    static_assert(is_convertible<_Up, value_type>::value,
                  "optional<T>::value_or: U must be convertible to T");
    return this->__engaged_ ? _VSTD::move(this->__val_) :
                              static_cast<value_type>(_VSTD::forward<_Up>(__v));
}

Specifically, notice how const& and && follow the method specifications. I don't think I've ever seen those applied this way, so what do they mean?

André Fratelli
  • 5,494
  • 4
  • 37
  • 80

0 Answers0