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