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