ledger-core
optional.hpp
1 // Copyright (C) 2011 - 2012 Andrzej Krzemienski.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // The idea and interface is based on Boost.Optional library
8 // authored by Fernando Luis Cacciola Carballal
9 
10 # ifndef ___OPTIONAL_HPP___
11 # define ___OPTIONAL_HPP___
12 
13 # include <utility>
14 # include <type_traits>
15 # include <initializer_list>
16 # include <cassert>
17 # include <functional>
18 # include <string>
19 # include <stdexcept>
20 # include <iostream>
21 # include <vector>
22 
23 # define TR2_OPTIONAL_REQUIRES(...) typename enable_if<__VA_ARGS__::value, bool>::type = false
24 
25 # if defined __GNUC__ // NOTE: GNUC is also defined for Clang
26 # if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)
27 # define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
28 # elif (__GNUC__ > 4)
29 # define TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
30 # endif
31 #
32 # if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)
33 # define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___
34 # elif (__GNUC__ > 4)
35 # define TR2_OPTIONAL_GCC_4_7_AND_HIGHER___
36 # endif
37 #
38 # if (__GNUC__ == 4) && (__GNUC_MINOR__ == 8) && (__GNUC_PATCHLEVEL__ >= 1)
39 # define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
40 # elif (__GNUC__ == 4) && (__GNUC_MINOR__ >= 9)
41 # define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
42 # elif (__GNUC__ > 4)
43 # define TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
44 # endif
45 # endif
46 #
47 # if defined __clang_major__
48 # if (__clang_major__ == 3 && __clang_minor__ >= 5)
49 # define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
50 # elif (__clang_major__ > 3)
51 # define TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
52 # endif
53 # if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_
54 # define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
55 # elif (__clang_major__ == 3 && __clang_minor__ == 4 && __clang_patchlevel__ >= 2)
56 # define TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
57 # endif
58 # endif
59 #
60 # if defined _MSC_VER
61 # if (_MSC_VER >= 1900)
62 # define TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
63 # endif
64 # endif
65 
66 # if defined __clang__
67 # if (__clang_major__ > 2) || (__clang_major__ == 2) && (__clang_minor__ >= 9)
68 # define OPTIONAL_HAS_THIS_RVALUE_REFS 1
69 # else
70 # define OPTIONAL_HAS_THIS_RVALUE_REFS 0
71 # endif
72 # elif defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
73 # define OPTIONAL_HAS_THIS_RVALUE_REFS 1
74 # elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
75 # define OPTIONAL_HAS_THIS_RVALUE_REFS 1
76 # else
77 # define OPTIONAL_HAS_THIS_RVALUE_REFS 0
78 # endif
79 
80 
81 # if defined TR2_OPTIONAL_GCC_4_8_1_AND_HIGHER___
82 # define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 1
83 # define OPTIONAL_CONSTEXPR_INIT_LIST constexpr
84 # else
85 # define OPTIONAL_HAS_CONSTEXPR_INIT_LIST 0
86 # define OPTIONAL_CONSTEXPR_INIT_LIST
87 # endif
88 
89 # if defined TR2_OPTIONAL_CLANG_3_5_AND_HIGHTER_ && (defined __cplusplus) && (__cplusplus != 201103L)
90 # define OPTIONAL_HAS_MOVE_ACCESSORS 1
91 # else
92 # define OPTIONAL_HAS_MOVE_ACCESSORS 0
93 # endif
94 
95 # // In C++11 constexpr implies const, so we need to make non-const members also non-constexpr
96 # if (defined __cplusplus) && (__cplusplus == 201103L)
97 # define OPTIONAL_MUTABLE_CONSTEXPR
98 # else
99 # define OPTIONAL_MUTABLE_CONSTEXPR constexpr
100 # endif
101 
102 namespace std{
103 
104  namespace experimental{
105 
106 // BEGIN workaround for missing is_trivially_destructible
107 # if defined TR2_OPTIONAL_GCC_4_8_AND_HIGHER___
108  // leave it: it is already there
109 # elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
110  // leave it: it is already there
111 # elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
112  // leave it: it is already there
113 # elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS
114  // leave it: the user doesn't want it
115 # else
116  template <typename T>
117  using is_trivially_destructible = std::has_trivial_destructor<T>;
118 # endif
119 // END workaround for missing is_trivially_destructible
120 
121 # if (defined TR2_OPTIONAL_GCC_4_7_AND_HIGHER___)
122  // leave it; our metafunctions are already defined.
123 # elif defined TR2_OPTIONAL_CLANG_3_4_2_AND_HIGHER_
124  // leave it; our metafunctions are already defined.
125 # elif defined TR2_OPTIONAL_MSVC_2015_AND_HIGHER___
126  // leave it: it is already there
127 # elif defined TR2_OPTIONAL_DISABLE_EMULATION_OF_TYPE_TRAITS
128  // leave it: the user doesn't want it
129 # else
130 
131 
132 // workaround for missing traits in GCC and CLANG
133 template <class T>
135 {
136  constexpr static bool value = std::is_nothrow_constructible<T, T&&>::value;
137 };
138 
139 
140 template <class T, class U>
142 {
143  template <class X, class Y>
144  constexpr static bool has_assign(...) { return false; }
145 
146  template <class X, class Y, size_t S = sizeof((std::declval<X>() = std::declval<Y>(), true)) >
147  // the comma operator is necessary for the cases where operator= returns void
148  constexpr static bool has_assign(bool) { return true; }
149 
150  constexpr static bool value = has_assign<T, U>(true);
151 };
152 
153 
154 template <class T>
156 {
157  template <class X, bool has_any_move_assign>
159  constexpr static bool value = false;
160  };
161 
162  template <class X>
163  struct has_nothrow_move_assign<X, true> {
164  constexpr static bool value = noexcept( std::declval<X&>() = std::declval<X&&>() );
165  };
166 
167  constexpr static bool value = has_nothrow_move_assign<T, is_assignable<T&, T&&>::value>::value;
168 };
169 // end workaround
170 
171 
172 # endif
173 
174 
175 
176 // 20.5.4, optional for object types
177  template <class T> class optional;
178 
179 // 20.5.5, optional for lvalue reference types
180  template <class T> class optional<T&>;
181 
182 
183 // workaround: std utility functions aren't constexpr yet
184  template <class T> inline constexpr T&& constexpr_forward(typename std::remove_reference<T>::type& t) noexcept
185  {
186  return static_cast<T&&>(t);
187  }
188 
189  template <class T> inline constexpr T&& constexpr_forward(typename std::remove_reference<T>::type&& t) noexcept
190  {
191  static_assert(!std::is_lvalue_reference<T>::value, "!!");
192  return static_cast<T&&>(t);
193  }
194 
195  template <class T> inline constexpr typename std::remove_reference<T>::type&& constexpr_move(T&& t) noexcept
196  {
197  return static_cast<typename std::remove_reference<T>::type&&>(t);
198  }
199 
200 
201 #if defined NDEBUG
202 # define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR)
203 #else
204 # define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) ((CHECK) ? (EXPR) : ([]{assert(!#CHECK);}(), (EXPR)))
205 #endif
206 
207 
208  namespace detail_
209  {
210 
211 // static_addressof: a constexpr version of addressof
212  template <typename T>
214  {
215  template <class X>
216  constexpr static bool has_overload(...) { return false; }
217 
218  template <class X, size_t S = sizeof(std::declval<X&>().operator&()) >
219  constexpr static bool has_overload(bool) { return true; }
220 
221  constexpr static bool value = has_overload<T>(true);
222  };
223 
224  template <typename T, TR2_OPTIONAL_REQUIRES(!has_overloaded_addressof<T>)>
225  constexpr T* static_addressof(T& ref)
226  {
227  return &ref;
228  }
229 
230  template <typename T, TR2_OPTIONAL_REQUIRES(has_overloaded_addressof<T>)>
231  T* static_addressof(T& ref)
232  {
233  return std::addressof(ref);
234  }
235 
236 
237 // the call to convert<A>(b) has return type A and converts b to type A iff b decltype(b) is implicitly convertible to A
238  template <class U>
239  constexpr U convert(U v) { return v; }
240 
241  } // namespace detail
242 
243 
244  constexpr struct trivial_init_t{} trivial_init{};
245 
246 
247 // 20.5.6, In-place construction
248  constexpr struct in_place_t{} in_place{};
249 
250 
251 // 20.5.7, Disengaged state indicator
252  struct nullopt_t
253  {
254  struct init{};
255  constexpr explicit nullopt_t(init){}
256  };
257  constexpr nullopt_t nullopt{nullopt_t::init()};
258 
259 
260 // 20.5.8, class bad_optional_access
261  class bad_optional_access : public logic_error {
262  public:
263  explicit bad_optional_access(const string& what_arg) : logic_error{what_arg} {}
264  explicit bad_optional_access(const char* what_arg) : logic_error{what_arg} {}
265  };
266 
267 
268  template <class T>
269  union storage_t
270  {
271  unsigned char dummy_;
272  T value_;
273 
274  constexpr storage_t( trivial_init_t ) noexcept : dummy_() {};
275 
276  template <class... Args>
277  constexpr storage_t( Args&&... args ) : value_(constexpr_forward<Args>(args)...) {}
278 
279  ~storage_t(){}
280  };
281 
282 
283  template <class T>
285  {
286  unsigned char dummy_;
287  T value_;
288 
289  constexpr constexpr_storage_t( trivial_init_t ) noexcept : dummy_() {};
290 
291  template <class... Args>
292  constexpr constexpr_storage_t( Args&&... args ) : value_(constexpr_forward<Args>(args)...) {}
293 
294  ~constexpr_storage_t() = default;
295  };
296 
297 
298  template <class T>
300  {
301  bool init_;
302  storage_t<T> storage_;
303 
304  constexpr optional_base() noexcept : init_(false), storage_(trivial_init) {};
305 
306  explicit constexpr optional_base(const T& v) : init_(true), storage_(v) {}
307 
308  explicit constexpr optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {}
309 
310  template <class... Args> explicit optional_base(in_place_t, Args&&... args)
311  : init_(true), storage_(constexpr_forward<Args>(args)...) {}
312 
313  template <class U, class... Args, TR2_OPTIONAL_REQUIRES(is_constructible<T, std::initializer_list<U>>)>
314  explicit optional_base(in_place_t, std::initializer_list<U> il, Args&&... args)
315  : init_(true), storage_(il, std::forward<Args>(args)...) {}
316 
317  ~optional_base() { if (init_) storage_.value_.T::~T(); }
318  };
319 
320 
321  template <class T>
323  {
324  bool init_;
325  constexpr_storage_t<T> storage_;
326 
327  constexpr constexpr_optional_base() noexcept : init_(false), storage_(trivial_init) {};
328 
329  explicit constexpr constexpr_optional_base(const T& v) : init_(true), storage_(v) {}
330 
331  explicit constexpr constexpr_optional_base(T&& v) : init_(true), storage_(constexpr_move(v)) {}
332 
333  template <class... Args> explicit constexpr constexpr_optional_base(in_place_t, Args&&... args)
334  : init_(true), storage_(constexpr_forward<Args>(args)...) {}
335 
336  template <class U, class... Args, TR2_OPTIONAL_REQUIRES(is_constructible<T, std::initializer_list<U>>)>
337  OPTIONAL_CONSTEXPR_INIT_LIST explicit constexpr_optional_base(in_place_t, std::initializer_list<U> il, Args&&... args)
338  : init_(true), storage_(il, std::forward<Args>(args)...) {}
339 
340  ~constexpr_optional_base() = default;
341  };
342 
343  template <class T>
344  using OptionalBase = typename std::conditional<
345  is_trivially_destructible<T>::value, // if possible
346  constexpr_optional_base<typename std::remove_const<T>::type>, // use base with trivial destructor
348  >::type;
349 
350 
351 
352  template <class T>
353  class optional : private OptionalBase<T>
354  {
355  static_assert( !std::is_same<typename std::decay<T>::type, nullopt_t>::value, "bad T" );
356  static_assert( !std::is_same<typename std::decay<T>::type, in_place_t>::value, "bad T" );
357 
358 
359  constexpr bool initialized() const noexcept { return OptionalBase<T>::init_; }
360  typename std::remove_const<T>::type* dataptr() { return std::addressof(OptionalBase<T>::storage_.value_); }
361  constexpr const T* dataptr() const { return detail_::static_addressof(OptionalBase<T>::storage_.value_); }
362 
363 # if OPTIONAL_HAS_THIS_RVALUE_REFS == 1
364  constexpr const T& contained_val() const& { return OptionalBase<T>::storage_.value_; }
365 # if OPTIONAL_HAS_MOVE_ACCESSORS == 1
366  OPTIONAL_MUTABLE_CONSTEXPR T&& contained_val() && { return std::move(OptionalBase<T>::storage_.value_); }
367  OPTIONAL_MUTABLE_CONSTEXPR T& contained_val() & { return OptionalBase<T>::storage_.value_; }
368 # else
369  T& contained_val() & { return OptionalBase<T>::storage_.value_; }
370  T&& contained_val() && { return std::move(OptionalBase<T>::storage_.value_); }
371 # endif
372 # else
373  constexpr const T& contained_val() const { return OptionalBase<T>::storage_.value_; }
374  T& contained_val() { return OptionalBase<T>::storage_.value_; }
375 # endif
376 
377  void clear() noexcept {
378  if (initialized()) dataptr()->T::~T();
379  OptionalBase<T>::init_ = false;
380  }
381 
382  template <class... Args>
383  void initialize(Args&&... args) noexcept(noexcept(T(std::forward<Args>(args)...)))
384  {
385  assert(!OptionalBase<T>::init_);
386  ::new (static_cast<void*>(dataptr())) T(std::forward<Args>(args)...);
387  OptionalBase<T>::init_ = true;
388  }
389 
390  template <class U, class... Args>
391  void initialize(std::initializer_list<U> il, Args&&... args) noexcept(noexcept(T(il, std::forward<Args>(args)...)))
392  {
393  assert(!OptionalBase<T>::init_);
394  ::new (static_cast<void*>(dataptr())) T(il, std::forward<Args>(args)...);
395  OptionalBase<T>::init_ = true;
396  }
397 
398  public:
399  typedef T value_type;
400 
401  // 20.5.5.1, constructors
402  constexpr optional() noexcept : OptionalBase<T>() {};
403  constexpr optional(nullopt_t) noexcept : OptionalBase<T>() {};
404 
405  optional(const optional& rhs)
406  : OptionalBase<T>()
407  {
408  if (rhs.initialized()) {
409  ::new (static_cast<void*>(dataptr())) T(*rhs);
410  OptionalBase<T>::init_ = true;
411  }
412  }
413 
415  : OptionalBase<T>()
416  {
417  if (rhs.initialized()) {
418  ::new (static_cast<void*>(dataptr())) T(std::move(*rhs));
419  OptionalBase<T>::init_ = true;
420  }
421  }
422 
423  constexpr optional(const T& v) : OptionalBase<T>(v) {}
424 
425  constexpr optional(T&& v) : OptionalBase<T>(constexpr_move(v)) {}
426 
427  template <class... Args>
428  explicit constexpr optional(in_place_t, Args&&... args)
429  : OptionalBase<T>(in_place_t{}, constexpr_forward<Args>(args)...) {}
430 
431  template <class U, class... Args, TR2_OPTIONAL_REQUIRES(is_constructible<T, std::initializer_list<U>>)>
432  OPTIONAL_CONSTEXPR_INIT_LIST explicit optional(in_place_t, std::initializer_list<U> il, Args&&... args)
433  : OptionalBase<T>(in_place_t{}, il, constexpr_forward<Args>(args)...) {}
434 
435  // 20.5.4.2, Destructor
436  ~optional() = default;
437 
438  // 20.5.4.3, assignment
439  optional& operator=(nullopt_t) noexcept
440  {
441  clear();
442  return *this;
443  }
444 
445  optional& operator=(const optional& rhs)
446  {
447  if (initialized() == true && rhs.initialized() == false) clear();
448  else if (initialized() == false && rhs.initialized() == true) initialize(*rhs);
449  else if (initialized() == true && rhs.initialized() == true) contained_val() = *rhs;
450  return *this;
451  }
452 
453  optional& operator=(optional&& rhs)
455  {
456  if (initialized() == true && rhs.initialized() == false) clear();
457  else if (initialized() == false && rhs.initialized() == true) initialize(std::move(*rhs));
458  else if (initialized() == true && rhs.initialized() == true) contained_val() = std::move(*rhs);
459  return *this;
460  }
461 
462  template <class U>
463  auto operator=(U&& v)
464  -> typename enable_if
465  <
466  is_same<typename decay<U>::type, T>::value,
467  optional&
468  >::type
469  {
470  if (initialized()) { contained_val() = std::forward<U>(v); }
471  else { initialize(std::forward<U>(v)); }
472  return *this;
473  }
474 
475 
476  template <class... Args>
477  void emplace(Args&&... args)
478  {
479  clear();
480  initialize(std::forward<Args>(args)...);
481  }
482 
483  template <class U, class... Args>
484  void emplace(initializer_list<U> il, Args&&... args)
485  {
486  clear();
487  initialize<U, Args...>(il, std::forward<Args>(args)...);
488  }
489 
490  // 20.5.4.4, Swap
491  void swap(optional<T>& rhs) noexcept(is_nothrow_move_constructible<T>::value && noexcept(swap(declval<T&>(), declval<T&>())))
492  {
493  if (initialized() == true && rhs.initialized() == false) { rhs.initialize(std::move(**this)); clear(); }
494  else if (initialized() == false && rhs.initialized() == true) { initialize(std::move(*rhs)); rhs.clear(); }
495  else if (initialized() == true && rhs.initialized() == true) { using std::swap; swap(**this, *rhs); }
496  }
497 
498  // 20.5.4.5, Observers
499 
500  explicit constexpr operator bool() const noexcept { return initialized(); }
501 
502  constexpr T const* operator ->() const {
503  return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), dataptr());
504  }
505 
506 # if OPTIONAL_HAS_MOVE_ACCESSORS == 1
507 
508  OPTIONAL_MUTABLE_CONSTEXPR T* operator ->() {
509  assert (initialized());
510  return dataptr();
511  }
512 
513  constexpr T const& operator *() const& {
514  return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val());
515  }
516 
517  OPTIONAL_MUTABLE_CONSTEXPR T& operator *() & {
518  assert (initialized());
519  return contained_val();
520  }
521 
522  OPTIONAL_MUTABLE_CONSTEXPR T&& operator *() && {
523  assert (initialized());
524  return constexpr_move(contained_val());
525  }
526 
527  constexpr T const& value() const& {
528  return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
529  }
530 
531  OPTIONAL_MUTABLE_CONSTEXPR T& value() & {
532  return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
533  }
534 
535  OPTIONAL_MUTABLE_CONSTEXPR T&& value() && {
536  if (!initialized()) throw bad_optional_access("bad optional access");
537  return std::move(contained_val());
538  }
539 
540 # else
541 
542  T* operator ->() {
543  assert (initialized());
544  return dataptr();
545  }
546 
547  constexpr T const& operator *() const {
548  return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val());
549  }
550 
551  T& operator *() {
552  assert (initialized());
553  return contained_val();
554  }
555 
556  constexpr T const& value() const {
557  return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
558  }
559 
560  T& value() {
561  return initialized() ? contained_val() : (throw bad_optional_access("bad optional access"), contained_val());
562  }
563 
564 # endif
565 
566 # if OPTIONAL_HAS_THIS_RVALUE_REFS == 1
567 
568  template <class V>
569  constexpr T value_or(V&& v) const&
570  {
571  return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
572  }
573 
574 # if OPTIONAL_HAS_MOVE_ACCESSORS == 1
575 
576  template <class V>
577  OPTIONAL_MUTABLE_CONSTEXPR T value_or(V&& v) &&
578  {
579  return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val()) : detail_::convert<T>(constexpr_forward<V>(v));
580  }
581 
582 # else
583 
584  template <class V>
585  T value_or(V&& v) &&
586  {
587  return *this ? constexpr_move(const_cast<optional<T>&>(*this).contained_val()) : detail_::convert<T>(constexpr_forward<V>(v));
588  }
589 
590 # endif
591 
592 # else
593 
594  template <class V>
595  constexpr T value_or(V&& v) const
596  {
597  return *this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
598  }
599 
600 # endif
601 
602  };
603 
604 
605  template <class T>
606  class optional<T&>
607  {
608  static_assert( !std::is_same<T, nullopt_t>::value, "bad T" );
609  static_assert( !std::is_same<T, in_place_t>::value, "bad T" );
610  T* ref;
611 
612  public:
613 
614  // 20.5.5.1, construction/destruction
615  constexpr optional() noexcept : ref(nullptr) {}
616 
617  constexpr optional(nullopt_t) noexcept : ref(nullptr) {}
618 
619  constexpr optional(T& v) noexcept : ref(detail_::static_addressof(v)) {}
620 
621  optional(T&&) = delete;
622 
623  constexpr optional(const optional& rhs) noexcept : ref(rhs.ref) {}
624 
625  explicit constexpr optional(in_place_t, T& v) noexcept : ref(detail_::static_addressof(v)) {}
626 
627  explicit optional(in_place_t, T&&) = delete;
628 
629  ~optional() = default;
630 
631  // 20.5.5.2, mutation
632  optional& operator=(nullopt_t) noexcept {
633  ref = nullptr;
634  return *this;
635  }
636 
637  friend ostream& operator<<(ostream& os, const optional<T>& o) {
638  if (!o) {
639  return os << "None";
640  } else {
641  return os << "Some(" << o.value() << ")";
642  }
643  }
644 
645  // optional& operator=(const optional& rhs) noexcept {
646  // ref = rhs.ref;
647  // return *this;
648  // }
649 
650  // optional& operator=(optional&& rhs) noexcept {
651  // ref = rhs.ref;
652  // return *this;
653  // }
654 
655  template <typename U>
656  auto operator=(U&& rhs) noexcept
657  -> typename enable_if
658  <
659  is_same<typename decay<U>::type, optional<T&>>::value,
660  optional&
661  >::type
662  {
663  ref = rhs.ref;
664  return *this;
665  }
666 
667  template <typename U>
668  auto operator=(U&& rhs) noexcept
669  -> typename enable_if
670  <
671  !is_same<typename decay<U>::type, optional<T&>>::value,
672  optional&
673  >::type
674  = delete;
675 
676  void emplace(T& v) noexcept {
677  ref = detail_::static_addressof(v);
678  }
679 
680  void emplace(T&&) = delete;
681 
682 
683  void swap(optional<T&>& rhs) noexcept
684  {
685  std::swap(ref, rhs.ref);
686  }
687 
688  // 20.5.5.3, observers
689  constexpr T* operator->() const {
690  return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, ref);
691  }
692 
693  constexpr T& operator*() const {
694  return TR2_OPTIONAL_ASSERTED_EXPRESSION(ref, *ref);
695  }
696 
697  constexpr T& value() const {
698  return ref ? *ref : (throw bad_optional_access("bad optional access"), *ref);
699  }
700 
701  explicit constexpr operator bool() const noexcept {
702  return ref != nullptr;
703  }
704 
705  template <class V>
706  constexpr typename decay<T>::type value_or(V&& v) const
707  {
708  return *this ? **this : detail_::convert<typename decay<T>::type>(constexpr_forward<V>(v));
709  }
710  };
711 
712 
713  template <class T>
714  class optional<T&&>
715  {
716  static_assert( sizeof(T) == 0, "optional rvalue references disallowed" );
717  };
718 
719 
720 // 20.5.8, Relational operators
721  template <class T> constexpr bool operator==(const optional<T>& x, const optional<T>& y)
722  {
723  return bool(x) != bool(y) ? false : bool(x) == false ? true : *x == *y;
724  }
725 
726  template <class T> constexpr bool operator!=(const optional<T>& x, const optional<T>& y)
727  {
728  return !(x == y);
729  }
730 
731  template <class T> constexpr bool operator<(const optional<T>& x, const optional<T>& y)
732  {
733  return (!y) ? false : (!x) ? true : *x < *y;
734  }
735 
736  template <class T> constexpr bool operator>(const optional<T>& x, const optional<T>& y)
737  {
738  return (y < x);
739  }
740 
741  template <class T> constexpr bool operator<=(const optional<T>& x, const optional<T>& y)
742  {
743  return !(y < x);
744  }
745 
746  template <class T> constexpr bool operator>=(const optional<T>& x, const optional<T>& y)
747  {
748  return !(x < y);
749  }
750 
751 
752 // 20.5.9, Comparison with nullopt
753  template <class T> constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept
754  {
755  return (!x);
756  }
757 
758  template <class T> constexpr bool operator==(nullopt_t, const optional<T>& x) noexcept
759  {
760  return (!x);
761  }
762 
763  template <class T> constexpr bool operator!=(const optional<T>& x, nullopt_t) noexcept
764  {
765  return bool(x);
766  }
767 
768  template <class T> constexpr bool operator!=(nullopt_t, const optional<T>& x) noexcept
769  {
770  return bool(x);
771  }
772 
773  template <class T> constexpr bool operator<(const optional<T>&, nullopt_t) noexcept
774  {
775  return false;
776  }
777 
778  template <class T> constexpr bool operator<(nullopt_t, const optional<T>& x) noexcept
779  {
780  return bool(x);
781  }
782 
783  template <class T> constexpr bool operator<=(const optional<T>& x, nullopt_t) noexcept
784  {
785  return (!x);
786  }
787 
788  template <class T> constexpr bool operator<=(nullopt_t, const optional<T>&) noexcept
789  {
790  return true;
791  }
792 
793  template <class T> constexpr bool operator>(const optional<T>& x, nullopt_t) noexcept
794  {
795  return bool(x);
796  }
797 
798  template <class T> constexpr bool operator>(nullopt_t, const optional<T>&) noexcept
799  {
800  return false;
801  }
802 
803  template <class T> constexpr bool operator>=(const optional<T>&, nullopt_t) noexcept
804  {
805  return true;
806  }
807 
808  template <class T> constexpr bool operator>=(nullopt_t, const optional<T>& x) noexcept
809  {
810  return (!x);
811  }
812 
813 
814 
815 // 20.5.10, Comparison with T
816  template <class T> constexpr bool operator==(const optional<T>& x, const T& v)
817  {
818  return bool(x) ? *x == v : false;
819  }
820 
821  template <class T> constexpr bool operator==(const T& v, const optional<T>& x)
822  {
823  return bool(x) ? v == *x : false;
824  }
825 
826  template <class T> constexpr bool operator!=(const optional<T>& x, const T& v)
827  {
828  return bool(x) ? *x != v : true;
829  }
830 
831  template <class T> constexpr bool operator!=(const T& v, const optional<T>& x)
832  {
833  return bool(x) ? v != *x : true;
834  }
835 
836  template <class T> constexpr bool operator<(const optional<T>& x, const T& v)
837  {
838  return bool(x) ? *x < v : true;
839  }
840 
841  template <class T> constexpr bool operator>(const T& v, const optional<T>& x)
842  {
843  return bool(x) ? v > *x : true;
844  }
845 
846  template <class T> constexpr bool operator>(const optional<T>& x, const T& v)
847  {
848  return bool(x) ? *x > v : false;
849  }
850 
851  template <class T> constexpr bool operator<(const T& v, const optional<T>& x)
852  {
853  return bool(x) ? v < *x : false;
854  }
855 
856  template <class T> constexpr bool operator>=(const optional<T>& x, const T& v)
857  {
858  return bool(x) ? *x >= v : false;
859  }
860 
861  template <class T> constexpr bool operator<=(const T& v, const optional<T>& x)
862  {
863  return bool(x) ? v <= *x : false;
864  }
865 
866  template <class T> constexpr bool operator<=(const optional<T>& x, const T& v)
867  {
868  return bool(x) ? *x <= v : true;
869  }
870 
871  template <class T> constexpr bool operator>=(const T& v, const optional<T>& x)
872  {
873  return bool(x) ? v >= *x : true;
874  }
875 
876 
877 // Comparison of optional<T&> with T
878  template <class T> constexpr bool operator==(const optional<T&>& x, const T& v)
879  {
880  return bool(x) ? *x == v : false;
881  }
882 
883  template <class T> constexpr bool operator==(const T& v, const optional<T&>& x)
884  {
885  return bool(x) ? v == *x : false;
886  }
887 
888  template <class T> constexpr bool operator!=(const optional<T&>& x, const T& v)
889  {
890  return bool(x) ? *x != v : true;
891  }
892 
893  template <class T> constexpr bool operator!=(const T& v, const optional<T&>& x)
894  {
895  return bool(x) ? v != *x : true;
896  }
897 
898  template <class T> constexpr bool operator<(const optional<T&>& x, const T& v)
899  {
900  return bool(x) ? *x < v : true;
901  }
902 
903  template <class T> constexpr bool operator>(const T& v, const optional<T&>& x)
904  {
905  return bool(x) ? v > *x : true;
906  }
907 
908  template <class T> constexpr bool operator>(const optional<T&>& x, const T& v)
909  {
910  return bool(x) ? *x > v : false;
911  }
912 
913  template <class T> constexpr bool operator<(const T& v, const optional<T&>& x)
914  {
915  return bool(x) ? v < *x : false;
916  }
917 
918  template <class T> constexpr bool operator>=(const optional<T&>& x, const T& v)
919  {
920  return bool(x) ? *x >= v : false;
921  }
922 
923  template <class T> constexpr bool operator<=(const T& v, const optional<T&>& x)
924  {
925  return bool(x) ? v <= *x : false;
926  }
927 
928  template <class T> constexpr bool operator<=(const optional<T&>& x, const T& v)
929  {
930  return bool(x) ? *x <= v : true;
931  }
932 
933  template <class T> constexpr bool operator>=(const T& v, const optional<T&>& x)
934  {
935  return bool(x) ? v >= *x : true;
936  }
937 
938 // Comparison of optional<T const&> with T
939  template <class T> constexpr bool operator==(const optional<const T&>& x, const T& v)
940  {
941  return bool(x) ? *x == v : false;
942  }
943 
944  template <class T> constexpr bool operator==(const T& v, const optional<const T&>& x)
945  {
946  return bool(x) ? v == *x : false;
947  }
948 
949  template <class T> constexpr bool operator!=(const optional<const T&>& x, const T& v)
950  {
951  return bool(x) ? *x != v : true;
952  }
953 
954  template <class T> constexpr bool operator!=(const T& v, const optional<const T&>& x)
955  {
956  return bool(x) ? v != *x : true;
957  }
958 
959  template <class T> constexpr bool operator<(const optional<const T&>& x, const T& v)
960  {
961  return bool(x) ? *x < v : true;
962  }
963 
964  template <class T> constexpr bool operator>(const T& v, const optional<const T&>& x)
965  {
966  return bool(x) ? v > *x : true;
967  }
968 
969  template <class T> constexpr bool operator>(const optional<const T&>& x, const T& v)
970  {
971  return bool(x) ? *x > v : false;
972  }
973 
974  template <class T> constexpr bool operator<(const T& v, const optional<const T&>& x)
975  {
976  return bool(x) ? v < *x : false;
977  }
978 
979  template <class T> constexpr bool operator>=(const optional<const T&>& x, const T& v)
980  {
981  return bool(x) ? *x >= v : false;
982  }
983 
984  template <class T> constexpr bool operator<=(const T& v, const optional<const T&>& x)
985  {
986  return bool(x) ? v <= *x : false;
987  }
988 
989  template <class T> constexpr bool operator<=(const optional<const T&>& x, const T& v)
990  {
991  return bool(x) ? *x <= v : true;
992  }
993 
994  template <class T> constexpr bool operator>=(const T& v, const optional<const T&>& x)
995  {
996  return bool(x) ? v >= *x : true;
997  }
998 
999  // 20.5.12, Specialized algorithms
1000  template <class T>
1001  void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y)))
1002  {
1003  x.swap(y);
1004  }
1005 
1006 
1007  template <class T>
1008  constexpr optional<typename decay<T>::type> make_optional(T&& v)
1009  {
1010  return optional<typename decay<T>::type>(constexpr_forward<T>(v));
1011  }
1012 
1013  template <class X>
1014  constexpr optional<X&> make_optional(reference_wrapper<X> v)
1015  {
1016  return optional<X&>(v.get());
1017  }
1018 
1019 
1020  } // namespace experimental
1021 } // namespace std
1022 
1023 namespace std
1024 {
1025  template <typename T>
1026  struct hash<std::experimental::optional<T>>
1027  {
1028  typedef typename hash<T>::result_type result_type;
1030 
1031  constexpr result_type operator()(argument_type const& arg) const {
1032  return arg ? std::hash<T>{}(*arg) : result_type{};
1033  }
1034  };
1035 
1036  template <typename T>
1037  struct hash<std::experimental::optional<T&>>
1038  {
1039  typedef typename hash<T>::result_type result_type;
1041 
1042  constexpr result_type operator()(argument_type const& arg) const {
1043  return arg ? std::hash<T>{}(*arg) : result_type{};
1044  }
1045  };
1046 }
1047 
1048 namespace ledger {
1049  namespace core {
1050  template <typename T>
1051  using optional = std::experimental::optional<T>;
1052  }
1053 }
1054 
1055 # undef TR2_OPTIONAL_REQUIRES
1056 # undef TR2_OPTIONAL_ASSERTED_EXPRESSION
1057 
1058 # endif //___OPTIONAL_HPP___
Definition: optional.hpp:269
Definition: optional.hpp:284
Definition: optional.hpp:252
Definition: BitcoinLikeFeePolicy.hpp:29
Definition: optional.hpp:254
Definition: optional.hpp:141
Definition: optional.hpp:248
Definition: optional.hpp:322
Definition: Account.cpp:8
Definition: optional.hpp:244
Definition: optional.hpp:299
Definition: optional.hpp:261
Definition: optional.hpp:177
Definition: optional.hpp:606