libstdc++
optional
Go to the documentation of this file.
1 // <optional> -*- C++ -*-
2 
3 // Copyright (C) 2013-2016 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file experimental/optional
26  * This is a TS C++ Library header.
27  */
28 
29 #ifndef _GLIBCXX_EXPERIMENTAL_OPTIONAL
30 #define _GLIBCXX_EXPERIMENTAL_OPTIONAL 1
31 
32 /**
33  * @defgroup experimental Experimental
34  *
35  * Components specified by various Technical Specifications.
36  *
37  * As indicated by the std::experimental namespace and the header paths,
38  * the contents of these Technical Specifications are experimental and not
39  * part of the C++ standard. As such the interfaces and implementations may
40  * change in the future, and there is <STRONG> no guarantee of compatibility
41  * between different GCC releases </STRONG> for these features.
42  */
43 
44 #if __cplusplus <= 201103L
45 # include <bits/c++14_warning.h>
46 #else
47 
48 #include <utility>
49 #include <type_traits>
50 #include <stdexcept>
51 #include <new>
52 #include <initializer_list>
53 #include <bits/functexcept.h>
54 #include <bits/functional_hash.h>
56 
57 namespace std _GLIBCXX_VISIBILITY(default)
58 {
59 namespace experimental
60 {
61 inline namespace fundamentals_v1
62 {
63 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64 
65  /**
66  * @defgroup optional Optional values
67  * @ingroup experimental
68  *
69  * Class template for optional values and surrounding facilities, as
70  * described in n3793 "A proposal to add a utility class to represent
71  * optional objects (Revision 5)".
72  *
73  * @{
74  */
75 
76 #define __cpp_lib_experimental_optional 201411
77 
78  // All subsequent [X.Y.n] references are against n3793.
79 
80  // [X.Y.4]
81  template<typename _Tp>
82  class optional;
83 
84  // [X.Y.5]
85  /// Tag type for in-place construction.
86  struct in_place_t { };
87 
88  /// Tag for in-place construction.
89  constexpr in_place_t in_place { };
90 
91  // [X.Y.6]
92  /// Tag type to disengage optional objects.
93  struct nullopt_t
94  {
95  // Do not user-declare default constructor at all for
96  // optional_value = {} syntax to work.
97  // nullopt_t() = delete;
98 
99  // Used for constructing nullopt.
100  enum class _Construct { _Token };
101 
102  // Must be constexpr for nullopt_t to be literal.
103  explicit constexpr nullopt_t(_Construct) { }
104  };
105 
106  // [X.Y.6]
107  /// Tag to disengage optional objects.
108  constexpr nullopt_t nullopt { nullopt_t::_Construct::_Token };
109 
110  // [X.Y.7]
111  /**
112  * @brief Exception class thrown when a disengaged optional object is
113  * dereferenced.
114  * @ingroup exceptions
115  */
117  {
118  public:
119  bad_optional_access() : logic_error("bad optional access") { }
120 
121  // XXX This constructor is non-standard. Should not be inline
122  explicit bad_optional_access(const char* __arg) : logic_error(__arg) { }
123 
124  virtual ~bad_optional_access() noexcept = default;
125  };
126 
127  void
128  __throw_bad_optional_access(const char*)
129  __attribute__((__noreturn__));
130 
131  // XXX Does not belong here.
132  inline void
133  __throw_bad_optional_access(const char* __s)
134  { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
135 
136  template<typename _Tp, typename = void>
137  struct _Has_addressof_mem : std::false_type { };
138 
139  template<typename _Tp>
140  struct _Has_addressof_mem<_Tp,
141  __void_t<decltype( std::declval<const _Tp&>().operator&() )>
142  >
143  : std::true_type { };
144 
145  template<typename _Tp, typename = void>
146  struct _Has_addressof_free : std::false_type { };
147 
148  template<typename _Tp>
149  struct _Has_addressof_free<_Tp,
150  __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
151  >
152  : std::true_type { };
153 
154  /**
155  * @brief Trait that detects the presence of an overloaded unary operator&.
156  *
157  * Practically speaking this detects the presence of such an operator when
158  * called on a const-qualified lvalue (i.e.
159  * declval<_Tp * const&>().operator&()).
160  */
161  template<typename _Tp>
163  : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
164  { };
165 
166  /**
167  * @brief An overload that attempts to take the address of an lvalue as a
168  * constant expression. Falls back to __addressof in the presence of an
169  * overloaded addressof operator (unary operator&), in which case the call
170  * will not be a constant expression.
171  */
172  template<typename _Tp, enable_if_t<!_Has_addressof<_Tp>::value, int>...>
173  constexpr _Tp* __constexpr_addressof(_Tp& __t)
174  { return &__t; }
175 
176  /**
177  * @brief Fallback overload that defers to __addressof.
178  */
179  template<typename _Tp, enable_if_t<_Has_addressof<_Tp>::value, int>...>
180  inline _Tp* __constexpr_addressof(_Tp& __t)
181  { return std::__addressof(__t); }
182 
183  /**
184  * @brief Class template that holds the necessary state for @ref optional
185  * and that has the responsibility for construction and the special members.
186  *
187  * Such a separate base class template is necessary in order to
188  * conditionally enable the special members (e.g. copy/move constructors).
189  * Note that this means that @ref _Optional_base implements the
190  * functionality for copy and move assignment, but not for converting
191  * assignment.
192  *
193  * @see optional, _Enable_special_members
194  */
195  template<typename _Tp, bool _ShouldProvideDestructor =
196  !is_trivially_destructible<_Tp>::value>
198  {
199  private:
200  // Remove const to avoid prohibition of reusing object storage for
201  // const-qualified types in [3.8/9]. This is strictly internal
202  // and even optional itself is oblivious to it.
203  using _Stored_type = remove_const_t<_Tp>;
204 
205  public:
206  // [X.Y.4.1] Constructors.
207 
208  // Constructors for disengaged optionals.
209  constexpr _Optional_base() noexcept
210  : _M_empty{} { }
211 
212  constexpr _Optional_base(nullopt_t) noexcept
213  : _Optional_base{} { }
214 
215  // Constructors for engaged optionals.
216  constexpr _Optional_base(const _Tp& __t)
217  : _M_payload(__t), _M_engaged(true) { }
218 
219  constexpr _Optional_base(_Tp&& __t)
220  : _M_payload(std::move(__t)), _M_engaged(true) { }
221 
222  template<typename... _Args>
223  constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
224  : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
225 
226  template<typename _Up, typename... _Args,
227  enable_if_t<is_constructible<_Tp,
229  _Args&&...>::value,
230  int>...>
231  constexpr explicit _Optional_base(in_place_t,
233  _Args&&... __args)
234  : _M_payload(__il, std::forward<_Args>(__args)...),
235  _M_engaged(true) { }
236 
237  // Copy and move constructors.
238  _Optional_base(const _Optional_base& __other)
239  {
240  if (__other._M_engaged)
241  this->_M_construct(__other._M_get());
242  }
243 
244  _Optional_base(_Optional_base&& __other)
245  noexcept(is_nothrow_move_constructible<_Tp>())
246  {
247  if (__other._M_engaged)
248  this->_M_construct(std::move(__other._M_get()));
249  }
250 
251  // [X.Y.4.3] (partly) Assignment.
252  _Optional_base&
253  operator=(const _Optional_base& __other)
254  {
255  if (this->_M_engaged && __other._M_engaged)
256  this->_M_get() = __other._M_get();
257  else
258  {
259  if (__other._M_engaged)
260  this->_M_construct(__other._M_get());
261  else
262  this->_M_reset();
263  }
264 
265  return *this;
266  }
267 
268  _Optional_base&
269  operator=(_Optional_base&& __other)
270  noexcept(__and_<is_nothrow_move_constructible<_Tp>,
271  is_nothrow_move_assignable<_Tp>>())
272  {
273  if (this->_M_engaged && __other._M_engaged)
274  this->_M_get() = std::move(__other._M_get());
275  else
276  {
277  if (__other._M_engaged)
278  this->_M_construct(std::move(__other._M_get()));
279  else
280  this->_M_reset();
281  }
282  return *this;
283  }
284 
285  // [X.Y.4.2] Destructor.
286  ~_Optional_base()
287  {
288  if (this->_M_engaged)
289  this->_M_payload.~_Stored_type();
290  }
291 
292  // The following functionality is also needed by optional, hence the
293  // protected accessibility.
294  protected:
295  constexpr bool _M_is_engaged() const noexcept
296  { return this->_M_engaged; }
297 
298  // The _M_get operations have _M_engaged as a precondition.
299  constexpr _Tp&
300  _M_get() noexcept
301  { return _M_payload; }
302 
303  constexpr const _Tp&
304  _M_get() const noexcept
305  { return _M_payload; }
306 
307  // The _M_construct operation has !_M_engaged as a precondition
308  // while _M_destruct has _M_engaged as a precondition.
309  template<typename... _Args>
310  void
311  _M_construct(_Args&&... __args)
312  noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
313  {
314  ::new (std::__addressof(this->_M_payload))
315  _Stored_type(std::forward<_Args>(__args)...);
316  this->_M_engaged = true;
317  }
318 
319  void
320  _M_destruct()
321  {
322  this->_M_engaged = false;
323  this->_M_payload.~_Stored_type();
324  }
325 
326  // _M_reset is a 'safe' operation with no precondition.
327  void
328  _M_reset()
329  {
330  if (this->_M_engaged)
331  this->_M_destruct();
332  }
333 
334  private:
335  struct _Empty_byte { };
336  union {
337  _Empty_byte _M_empty;
338  _Stored_type _M_payload;
339  };
340  bool _M_engaged = false;
341  };
342 
343  /// Partial specialization that is exactly identical to the primary template
344  /// save for not providing a destructor, to fulfill triviality requirements.
345  template<typename _Tp>
346  class _Optional_base<_Tp, false>
347  {
348  private:
349  using _Stored_type = remove_const_t<_Tp>;
350 
351  public:
352  constexpr _Optional_base() noexcept
353  : _M_empty{} { }
354 
355  constexpr _Optional_base(nullopt_t) noexcept
356  : _Optional_base{} { }
357 
358  constexpr _Optional_base(const _Tp& __t)
359  : _M_payload(__t), _M_engaged(true) { }
360 
361  constexpr _Optional_base(_Tp&& __t)
362  : _M_payload(std::move(__t)), _M_engaged(true) { }
363 
364  template<typename... _Args>
365  constexpr explicit _Optional_base(in_place_t, _Args&&... __args)
366  : _M_payload(std::forward<_Args>(__args)...), _M_engaged(true) { }
367 
368  template<typename _Up, typename... _Args,
369  enable_if_t<is_constructible<_Tp,
371  _Args&&...>::value,
372  int>...>
373  constexpr explicit _Optional_base(in_place_t,
375  _Args&&... __args)
376  : _M_payload(__il, std::forward<_Args>(__args)...),
377  _M_engaged(true) { }
378 
379  _Optional_base(const _Optional_base& __other)
380  {
381  if (__other._M_engaged)
382  this->_M_construct(__other._M_get());
383  }
384 
385  _Optional_base(_Optional_base&& __other)
386  noexcept(is_nothrow_move_constructible<_Tp>())
387  {
388  if (__other._M_engaged)
389  this->_M_construct(std::move(__other._M_get()));
390  }
391 
392  _Optional_base&
393  operator=(const _Optional_base& __other)
394  {
395  if (this->_M_engaged && __other._M_engaged)
396  this->_M_get() = __other._M_get();
397  else
398  {
399  if (__other._M_engaged)
400  this->_M_construct(__other._M_get());
401  else
402  this->_M_reset();
403  }
404  return *this;
405  }
406 
407  _Optional_base&
408  operator=(_Optional_base&& __other)
409  noexcept(__and_<is_nothrow_move_constructible<_Tp>,
410  is_nothrow_move_assignable<_Tp>>())
411  {
412  if (this->_M_engaged && __other._M_engaged)
413  this->_M_get() = std::move(__other._M_get());
414  else
415  {
416  if (__other._M_engaged)
417  this->_M_construct(std::move(__other._M_get()));
418  else
419  this->_M_reset();
420  }
421  return *this;
422  }
423 
424  // Sole difference
425  // ~_Optional_base() noexcept = default;
426 
427  protected:
428  constexpr bool _M_is_engaged() const noexcept
429  { return this->_M_engaged; }
430 
431  _Tp&
432  _M_get() noexcept
433  { return _M_payload; }
434 
435  constexpr const _Tp&
436  _M_get() const noexcept
437  { return _M_payload; }
438 
439  template<typename... _Args>
440  void
441  _M_construct(_Args&&... __args)
442  noexcept(is_nothrow_constructible<_Stored_type, _Args...>())
443  {
444  ::new (std::__addressof(this->_M_payload))
445  _Stored_type(std::forward<_Args>(__args)...);
446  this->_M_engaged = true;
447  }
448 
449  void
450  _M_destruct()
451  {
452  this->_M_engaged = false;
453  this->_M_payload.~_Stored_type();
454  }
455 
456  void
457  _M_reset()
458  {
459  if (this->_M_engaged)
460  this->_M_destruct();
461  }
462 
463  private:
464  struct _Empty_byte { };
465  union
466  {
467  _Empty_byte _M_empty;
468  _Stored_type _M_payload;
469  };
470  bool _M_engaged = false;
471  };
472 
473  template<typename _Tp>
474  class optional;
475 
476  template<typename>
477  struct __is_optional_impl : false_type
478  { };
479 
480  template<typename _Tp>
481  struct __is_optional_impl<optional<_Tp>> : true_type
482  { };
483 
484  template<typename _Tp>
485  struct __is_optional
486  : public __is_optional_impl<std::remove_cv_t<std::remove_reference_t<_Tp>>>
487  { };
488 
489 
490  /**
491  * @brief Class template for optional values.
492  */
493  template<typename _Tp>
494  class optional
495  : private _Optional_base<_Tp>,
496  private _Enable_copy_move<
497  // Copy constructor.
498  is_copy_constructible<_Tp>::value,
499  // Copy assignment.
500  __and_<is_copy_constructible<_Tp>, is_copy_assignable<_Tp>>::value,
501  // Move constructor.
502  is_move_constructible<_Tp>::value,
503  // Move assignment.
504  __and_<is_move_constructible<_Tp>, is_move_assignable<_Tp>>::value,
505  // Unique tag type.
506  optional<_Tp>>
507  {
508  static_assert(__and_<__not_<is_same<remove_cv_t<_Tp>, nullopt_t>>,
509  __not_<is_same<remove_cv_t<_Tp>, in_place_t>>,
510  __not_<is_reference<_Tp>>>(),
511  "Invalid instantiation of optional<T>");
512 
513  private:
514  using _Base = _Optional_base<_Tp>;
515 
516  public:
517  using value_type = _Tp;
518 
519  // _Optional_base has the responsibility for construction.
520  using _Base::_Base;
521 
522  constexpr optional() = default;
523  // Converting constructors for engaged optionals.
524  template <typename _Up,
525  enable_if_t<__and_<
526  __not_<is_same<_Tp, _Up>>,
527  is_constructible<_Tp, _Up&&>,
528  is_convertible<_Up&&, _Tp>
529  >::value, bool> = true>
530  constexpr optional(_Up&& __t)
531  : _Base(_Tp(std::forward<_Up>(__t))) { }
532 
533  template <typename _Up,
534  enable_if_t<__and_<
535  __not_<is_same<_Tp, _Up>>,
536  is_constructible<_Tp, _Up&&>,
537  __not_<is_convertible<_Up&&, _Tp>>
538  >::value, bool> = false>
539  explicit constexpr optional(_Up&& __t)
540  : _Base(_Tp(std::forward<_Up>(__t))) { }
541 
542  template <typename _Up,
543  enable_if_t<__and_<
544  __not_<is_same<_Tp, _Up>>,
545  __not_<is_constructible<
546  _Tp, const optional<_Up>&>>,
547  __not_<is_convertible<
548  const optional<_Up>&, _Tp>>,
549  is_constructible<_Tp, const _Up&>,
550  is_convertible<const _Up&, _Tp>
551  >::value, bool> = true>
552  constexpr optional(const optional<_Up>& __t)
553  : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
554 
555  template <typename _Up,
556  enable_if_t<__and_<
557  __not_<is_same<_Tp, _Up>>,
558  __not_<is_constructible<
559  _Tp, const optional<_Up>&>>,
560  __not_<is_convertible<
561  const optional<_Up>&, _Tp>>,
562  is_constructible<_Tp, const _Up&>,
563  __not_<is_convertible<const _Up&, _Tp>>
564  >::value, bool> = false>
565  explicit constexpr optional(const optional<_Up>& __t)
566  : _Base(__t ? optional<_Tp>(*__t) : optional<_Tp>()) { }
567 
568  template <typename _Up,
569  enable_if_t<__and_<
570  __not_<is_same<_Tp, _Up>>,
571  __not_<is_constructible<
572  _Tp, optional<_Up>&&>>,
573  __not_<is_convertible<
574  optional<_Up>&&, _Tp>>,
575  is_constructible<_Tp, _Up&&>,
576  is_convertible<_Up&&, _Tp>
577  >::value, bool> = true>
578  constexpr optional(optional<_Up>&& __t)
579  : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
580 
581  template <typename _Up,
582  enable_if_t<__and_<
583  __not_<is_same<_Tp, _Up>>,
584  __not_<is_constructible<
585  _Tp, optional<_Up>&&>>,
586  __not_<is_convertible<
587  optional<_Up>&&, _Tp>>,
588  is_constructible<_Tp, _Up&&>,
589  __not_<is_convertible<_Up&&, _Tp>>
590  >::value, bool> = false>
591  explicit constexpr optional(optional<_Up>&& __t)
592  : _Base(__t ? optional<_Tp>(std::move(*__t)) : optional<_Tp>()) { }
593 
594  // [X.Y.4.3] (partly) Assignment.
595  optional&
596  operator=(nullopt_t) noexcept
597  {
598  this->_M_reset();
599  return *this;
600  }
601 
602  template<typename _Up,
603  enable_if_t<__and_<
604  __not_<is_same<_Up, nullopt_t>>,
605  __not_<__is_optional<_Up>>>::value,
606  bool> = true>
607  optional&
608  operator=(_Up&& __u)
609  {
610  static_assert(__and_<is_constructible<_Tp, _Up>,
611  is_assignable<_Tp&, _Up>>(),
612  "Cannot assign to value type from argument");
613 
614  if (this->_M_is_engaged())
615  this->_M_get() = std::forward<_Up>(__u);
616  else
617  this->_M_construct(std::forward<_Up>(__u));
618 
619  return *this;
620  }
621 
622  template<typename _Up,
623  enable_if_t<__and_<
624  __not_<is_same<_Tp, _Up>>>::value,
625  bool> = true>
626  optional&
627  operator=(const optional<_Up>& __u)
628  {
629  static_assert(__and_<is_constructible<_Tp, _Up>,
630  is_assignable<_Tp&, _Up>>(),
631  "Cannot assign to value type from argument");
632 
633  if (__u)
634  {
635  if (this->_M_is_engaged())
636  this->_M_get() = *__u;
637  else
638  this->_M_construct(*__u);
639  }
640  else
641  {
642  this->_M_reset();
643  }
644  return *this;
645  }
646 
647  template<typename _Up,
648  enable_if_t<__and_<
649  __not_<is_same<_Tp, _Up>>>::value,
650  bool> = true>
651  optional&
652  operator=(optional<_Up>&& __u)
653  {
654  static_assert(__and_<is_constructible<_Tp, _Up>,
655  is_assignable<_Tp&, _Up>>(),
656  "Cannot assign to value type from argument");
657 
658  if (__u)
659  {
660  if (this->_M_is_engaged())
661  this->_M_get() = std::move(*__u);
662  else
663  this->_M_construct(std::move(*__u));
664  }
665  else
666  {
667  this->_M_reset();
668  }
669 
670  return *this;
671  }
672 
673  template<typename... _Args>
674  void
675  emplace(_Args&&... __args)
676  {
677  static_assert(is_constructible<_Tp, _Args&&...>(),
678  "Cannot emplace value type from arguments");
679 
680  this->_M_reset();
681  this->_M_construct(std::forward<_Args>(__args)...);
682  }
683 
684  template<typename _Up, typename... _Args>
685  enable_if_t<is_constructible<_Tp, initializer_list<_Up>&,
686  _Args&&...>::value>
687  emplace(initializer_list<_Up> __il, _Args&&... __args)
688  {
689  this->_M_reset();
690  this->_M_construct(__il, std::forward<_Args>(__args)...);
691  }
692 
693  // [X.Y.4.2] Destructor is implicit, implemented in _Optional_base.
694 
695  // [X.Y.4.4] Swap.
696  void
697  swap(optional& __other)
698  noexcept(is_nothrow_move_constructible<_Tp>()
699  && noexcept(swap(declval<_Tp&>(), declval<_Tp&>())))
700  {
701  using std::swap;
702 
703  if (this->_M_is_engaged() && __other._M_is_engaged())
704  swap(this->_M_get(), __other._M_get());
705  else if (this->_M_is_engaged())
706  {
707  __other._M_construct(std::move(this->_M_get()));
708  this->_M_destruct();
709  }
710  else if (__other._M_is_engaged())
711  {
712  this->_M_construct(std::move(__other._M_get()));
713  __other._M_destruct();
714  }
715  }
716 
717  // [X.Y.4.5] Observers.
718  constexpr const _Tp*
719  operator->() const
720  { return __constexpr_addressof(this->_M_get()); }
721 
722  _Tp*
723  operator->()
724  { return std::__addressof(this->_M_get()); }
725 
726  constexpr const _Tp&
727  operator*() const&
728  { return this->_M_get(); }
729 
730  constexpr _Tp&
731  operator*()&
732  { return this->_M_get(); }
733 
734  constexpr _Tp&&
735  operator*()&&
736  { return std::move(this->_M_get()); }
737 
738  constexpr const _Tp&&
739  operator*() const&&
740  { return std::move(this->_M_get()); }
741 
742  constexpr explicit operator bool() const noexcept
743  { return this->_M_is_engaged(); }
744 
745  constexpr const _Tp&
746  value() const&
747  {
748  return this->_M_is_engaged()
749  ? this->_M_get()
750  : (__throw_bad_optional_access("Attempt to access value of a "
751  "disengaged optional object"),
752  this->_M_get());
753  }
754 
755  constexpr _Tp&
756  value()&
757  {
758  return this->_M_is_engaged()
759  ? this->_M_get()
760  : (__throw_bad_optional_access("Attempt to access value of a "
761  "disengaged optional object"),
762  this->_M_get());
763  }
764 
765  constexpr _Tp&&
766  value()&&
767  {
768  return this->_M_is_engaged()
769  ? std::move(this->_M_get())
770  : (__throw_bad_optional_access("Attempt to access value of a "
771  "disengaged optional object"),
772  std::move(this->_M_get()));
773  }
774 
775  constexpr const _Tp&&
776  value() const&&
777  {
778  return this->_M_is_engaged()
779  ? std::move(this->_M_get())
780  : (__throw_bad_optional_access("Attempt to access value of a "
781  "disengaged optional object"),
782  std::move(this->_M_get()));
783  }
784 
785  template<typename _Up>
786  constexpr _Tp
787  value_or(_Up&& __u) const&
788  {
789  static_assert(__and_<is_copy_constructible<_Tp>,
790  is_convertible<_Up&&, _Tp>>(),
791  "Cannot return value");
792 
793  return this->_M_is_engaged()
794  ? this->_M_get()
795  : static_cast<_Tp>(std::forward<_Up>(__u));
796  }
797 
798  template<typename _Up>
799  _Tp
800  value_or(_Up&& __u) &&
801  {
802  static_assert(__and_<is_move_constructible<_Tp>,
803  is_convertible<_Up&&, _Tp>>(),
804  "Cannot return value" );
805 
806  return this->_M_is_engaged()
807  ? std::move(this->_M_get())
808  : static_cast<_Tp>(std::forward<_Up>(__u));
809  }
810  };
811 
812  // [X.Y.8] Comparisons between optional values.
813  template<typename _Tp>
814  constexpr bool
815  operator==(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
816  {
817  return static_cast<bool>(__lhs) == static_cast<bool>(__rhs)
818  && (!__lhs || *__lhs == *__rhs);
819  }
820 
821  template<typename _Tp>
822  constexpr bool
823  operator!=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
824  { return !(__lhs == __rhs); }
825 
826  template<typename _Tp>
827  constexpr bool
828  operator<(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
829  {
830  return static_cast<bool>(__rhs) && (!__lhs || *__lhs < *__rhs);
831  }
832 
833  template<typename _Tp>
834  constexpr bool
835  operator>(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
836  { return __rhs < __lhs; }
837 
838  template<typename _Tp>
839  constexpr bool
840  operator<=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
841  { return !(__rhs < __lhs); }
842 
843  template<typename _Tp>
844  constexpr bool
845  operator>=(const optional<_Tp>& __lhs, const optional<_Tp>& __rhs)
846  { return !(__lhs < __rhs); }
847 
848  // [X.Y.9] Comparisons with nullopt.
849  template<typename _Tp>
850  constexpr bool
851  operator==(const optional<_Tp>& __lhs, nullopt_t) noexcept
852  { return !__lhs; }
853 
854  template<typename _Tp>
855  constexpr bool
856  operator==(nullopt_t, const optional<_Tp>& __rhs) noexcept
857  { return !__rhs; }
858 
859  template<typename _Tp>
860  constexpr bool
861  operator!=(const optional<_Tp>& __lhs, nullopt_t) noexcept
862  { return static_cast<bool>(__lhs); }
863 
864  template<typename _Tp>
865  constexpr bool
866  operator!=(nullopt_t, const optional<_Tp>& __rhs) noexcept
867  { return static_cast<bool>(__rhs); }
868 
869  template<typename _Tp>
870  constexpr bool
871  operator<(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
872  { return false; }
873 
874  template<typename _Tp>
875  constexpr bool
876  operator<(nullopt_t, const optional<_Tp>& __rhs) noexcept
877  { return static_cast<bool>(__rhs); }
878 
879  template<typename _Tp>
880  constexpr bool
881  operator>(const optional<_Tp>& __lhs, nullopt_t) noexcept
882  { return static_cast<bool>(__lhs); }
883 
884  template<typename _Tp>
885  constexpr bool
886  operator>(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
887  { return false; }
888 
889  template<typename _Tp>
890  constexpr bool
891  operator<=(const optional<_Tp>& __lhs, nullopt_t) noexcept
892  { return !__lhs; }
893 
894  template<typename _Tp>
895  constexpr bool
896  operator<=(nullopt_t, const optional<_Tp>& /* __rhs */) noexcept
897  { return true; }
898 
899  template<typename _Tp>
900  constexpr bool
901  operator>=(const optional<_Tp>& /* __lhs */, nullopt_t) noexcept
902  { return true; }
903 
904  template<typename _Tp>
905  constexpr bool
906  operator>=(nullopt_t, const optional<_Tp>& __rhs) noexcept
907  { return !__rhs; }
908 
909  // [X.Y.10] Comparisons with value type.
910  template<typename _Tp>
911  constexpr bool
912  operator==(const optional<_Tp>& __lhs, const _Tp& __rhs)
913  { return __lhs && *__lhs == __rhs; }
914 
915  template<typename _Tp>
916  constexpr bool
917  operator==(const _Tp& __lhs, const optional<_Tp>& __rhs)
918  { return __rhs && __lhs == *__rhs; }
919 
920  template<typename _Tp>
921  constexpr bool
922  operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
923  { return !__lhs || !(*__lhs == __rhs); }
924 
925  template<typename _Tp>
926  constexpr bool
927  operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
928  { return !__rhs || !(__lhs == *__rhs); }
929 
930  template<typename _Tp>
931  constexpr bool
932  operator<(const optional<_Tp>& __lhs, const _Tp& __rhs)
933  { return !__lhs || *__lhs < __rhs; }
934 
935  template<typename _Tp>
936  constexpr bool
937  operator<(const _Tp& __lhs, const optional<_Tp>& __rhs)
938  { return __rhs && __lhs < *__rhs; }
939 
940  template<typename _Tp>
941  constexpr bool
942  operator>(const optional<_Tp>& __lhs, const _Tp& __rhs)
943  { return __lhs && __rhs < *__lhs; }
944 
945  template<typename _Tp>
946  constexpr bool
947  operator>(const _Tp& __lhs, const optional<_Tp>& __rhs)
948  { return !__rhs || *__rhs < __lhs; }
949 
950  template<typename _Tp>
951  constexpr bool
952  operator<=(const optional<_Tp>& __lhs, const _Tp& __rhs)
953  { return !__lhs || !(__rhs < *__lhs); }
954 
955  template<typename _Tp>
956  constexpr bool
957  operator<=(const _Tp& __lhs, const optional<_Tp>& __rhs)
958  { return __rhs && !(*__rhs < __lhs); }
959 
960  template<typename _Tp>
961  constexpr bool
962  operator>=(const optional<_Tp>& __lhs, const _Tp& __rhs)
963  { return __lhs && !(*__lhs < __rhs); }
964 
965  template<typename _Tp>
966  constexpr bool
967  operator>=(const _Tp& __lhs, const optional<_Tp>& __rhs)
968  { return !__rhs || !(__lhs < *__rhs); }
969 
970  // [X.Y.11]
971  template<typename _Tp>
972  inline void
973  swap(optional<_Tp>& __lhs, optional<_Tp>& __rhs)
974  noexcept(noexcept(__lhs.swap(__rhs)))
975  { __lhs.swap(__rhs); }
976 
977  template<typename _Tp>
978  constexpr optional<decay_t<_Tp>>
979  make_optional(_Tp&& __t)
980  { return optional<decay_t<_Tp>> { std::forward<_Tp>(__t) }; }
981 
982  // @} group optional
983 _GLIBCXX_END_NAMESPACE_VERSION
984 } // namespace fundamentals_v1
985 }
986 
987  // [X.Y.12]
988  template<typename _Tp>
989  struct hash<experimental::optional<_Tp>>
990  {
991  using result_type = size_t;
992  using argument_type = experimental::optional<_Tp>;
993 
994  size_t
995  operator()(const experimental::optional<_Tp>& __t) const
996  noexcept(noexcept(hash<_Tp> {}(*__t)))
997  {
998  // We pick an arbitrary hash for disengaged optionals which hopefully
999  // usual values of _Tp won't typically hash to.
1000  constexpr size_t __magic_disengaged_hash = static_cast<size_t>(-3333);
1001  return __t ? hash<_Tp> {}(*__t) : __magic_disengaged_hash;
1002  }
1003  };
1004 }
1005 
1006 #endif // C++14
1007 
1008 #endif // _GLIBCXX_EXPERIMENTAL_OPTIONAL
constexpr in_place_t in_place
Tag for in-place construction.
Definition: optional:89
Tag type for in-place construction.
Definition: optional:86
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:47
Trait that detects the presence of an overloaded unary operator&amp;.
Definition: optional:162
initializer_list
Class template for optional values.
Definition: optional:82
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:76
logic_error(const string &__arg) _GLIBCXX_TXN_SAFE
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
Definition: fstream:1052
constexpr nullopt_t nullopt
Tag to disengage optional objects.
Definition: optional:108
integral_constant
Definition: type_traits:69
One of two subclasses of exception.
Definition: stdexcept:113
constexpr _Tp * __constexpr_addressof(_Tp &__t)
An overload that attempts to take the address of an lvalue as a constant expression. Falls back to __addressof in the presence of an overloaded addressof operator (unary operator&amp;), in which case the call will not be a constant expression.
Definition: optional:173
Exception class thrown when a disengaged optional object is dereferenced.
Definition: optional:116
Class template that holds the necessary state for Optional values and that has the responsibility for...
Definition: optional:197
Tag type to disengage optional objects.
Definition: optional:93