1 #ifndef DUNE_COMMON_STD_OPTIONAL_HH
2 #define DUNE_COMMON_STD_OPTIONAL_HH
10 #ifdef DUNE_HAVE_CXX_OPTIONAL
12 #endif // #ifdef DUNE_HAVE_CXX_OPTIONAL
21 #ifdef DUNE_HAVE_CXX_OPTIONAL
24 using optional = std::optional< T >;
61 :
public std::logic_error
89 constexpr
optional () noexcept : engaged_( false ) {}
94 std::enable_if_t< std::is_constructible< value_type, U&& >::value,
int > = 0,
95 std::enable_if_t< !std::is_convertible< U&&, value_type >::value,
int > = 0 >
97 : engaged_( true ),
value_( std::forward< U >(
value ) )
101 std::enable_if_t< std::is_constructible< value_type, U&& >::value,
int > = 0,
102 std::enable_if_t< std::is_convertible< U&&, value_type >::value,
int > = 0 >
104 : engaged_( true ),
value_( std::forward< U >(
value ) )
110 template<
class... Args >
112 : engaged_( true ),
value_( std::forward< Args >( args )... )
122 optional (
const optional &other ) noexcept( std::is_nothrow_copy_constructible< T >::value )
123 : engaged_( other.engaged_ )
130 : engaged_( other.engaged_ )
137 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
138 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
139 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
140 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
141 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
142 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
143 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
144 std::enable_if_t< !std::is_convertible< const U&, value_type >::value,
int > = 0 >
146 : engaged_( other.engaged_ )
153 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
154 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
155 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
156 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
157 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
158 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
159 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
160 std::enable_if_t< std::is_convertible< const U&, value_type >::value,
int > = 0 >
162 : engaged_( other.engaged_ )
169 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
170 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
171 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
172 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
173 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
174 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
175 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
176 std::enable_if_t< !std::is_convertible< const U&, value_type >::value,
int > = 0 >
178 : engaged_( other.engaged_ )
185 std::enable_if_t< std::is_constructible< value_type, const U& >::value,
int > = 0,
186 std::enable_if_t< !std::is_constructible< value_type, optional< U >& >
::value,
int > = 0,
187 std::enable_if_t< !std::is_constructible< value_type, const optional< U >& >
::value,
int > = 0,
188 std::enable_if_t< !std::is_constructible< value_type, optional< U >&& >
::value,
int > = 0,
189 std::enable_if_t< !std::is_constructible< optional< U >&,
value_type >
::value,
int > = 0,
190 std::enable_if_t< !std::is_constructible< const optional< U >&,
value_type >
::value,
int > = 0,
191 std::enable_if_t< !std::is_constructible< optional< U >&&,
value_type >
::value,
int > = 0,
192 std::enable_if_t< std::is_convertible< const U&, value_type >::value,
int > = 0 >
194 : engaged_( other.engaged_ )
208 optional &
operator= (
const optional &other ) noexcept( std::is_nothrow_copy_constructible< T >::value && std::is_nothrow_copy_assignable< T >::value )
217 else if( other.engaged_ )
219 engaged_ = other.engaged_;
223 optional &
operator= (
optional &&other ) noexcept( std::is_nothrow_move_constructible< T >::value && std::is_nothrow_move_assignable< T >::value )
228 value_ = std::move( other.value_ );
232 else if( other.engaged_ )
234 engaged_ = other.engaged_;
238 template<
class U = value_type >
239 typename std::enable_if< std::is_constructible< value_type, U >::value && std::is_assignable< value_type, U >::value,
optional & >::type
264 explicit constexpr
operator bool () const noexcept {
return engaged_; }
305 template<
class... Args >
324 void swap (
optional &other ) noexcept( std::is_nothrow_move_constructible< T >::value && noexcept( std::swap( std::declval< T & >(), std::declval< T & >() ) ) )
326 std::swap( engaged_, other.engaged_ );
330 std::swap(
value_, other.value_ );
334 other.value_.~value_type();
337 else if( other.engaged_ )
359 return (lhs && rhs ? *lhs == *rhs :
static_cast< bool >( lhs ) ==
static_cast< bool >( rhs ));
366 return (rhs && (lhs ? std::less< T >()( *lhs, *rhs ) :
true));
391 return static_cast< bool >( rhs );
397 return (lhs && (*lhs == rhs));
403 return (rhs && (lhs == *rhs));
409 return (lhs ? std::less< T >()( *lhs, rhs ) :
true);
415 return (rhs ? std::less< T >()( lhs, *rhs ) :
false);
429 #endif //#ifdef DUNE_HAVE_CXX_OPTIONAL
436 #ifndef DUNE_HAVE_CXX_OPTIONAL
455 struct hash<
Dune::Std::optional< T > >
459 return (arg ? std::hash< T >()( arg ) : 0);
465 #endif //#ifndef DUNE_HAVE_CXX_OPTIONAL
467 #endif // #ifndef DUNE_COMMON_STD_OPTIONAL_HH