proxsuite 0.7.3
The Advanced Proximal Optimization Toolbox
Loading...
Searching...
No Matches
tl-optional.hpp
Go to the documentation of this file.
1
3// optional - An implementation of std::optional with extensions
4// Written in 2017 by Sy Brand (tartanllama@gmail.com, @TartanLlama)
5//
6// Documentation available at https://tl.tartanllama.xyz/
7//
8// To the extent possible under law, the author(s) have dedicated all
9// copyright and related and neighboring rights to this software to the
10// public domain worldwide. This software is distributed without any warranty.
11//
12// You should have received a copy of the CC0 Public Domain Dedication
13// along with this software. If not, see
14// <http://creativecommons.org/publicdomain/zero/1.0/>.
16
17#ifndef TL_OPTIONAL_HPP
18#define TL_OPTIONAL_HPP
19
20#define TL_OPTIONAL_VERSION_MAJOR 1
21#define TL_OPTIONAL_VERSION_MINOR 1
22#define TL_OPTIONAL_VERSION_PATCH 0
23
24#include <new>
25#include <exception>
26#include <functional>
27#include <type_traits>
28#include <utility>
29
30#if (defined(_MSC_VER) && _MSC_VER == 1900)
31#define TL_OPTIONAL_MSVC2015
32#endif
33
34#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \
35 !defined(__clang__))
36#define TL_OPTIONAL_GCC49
37#endif
38
39#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && \
40 !defined(__clang__))
41#define TL_OPTIONAL_GCC54
42#endif
43
44#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && \
45 !defined(__clang__))
46#define TL_OPTIONAL_GCC55
47#endif
48
49#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && \
50 !defined(__clang__))
51// GCC < 5 doesn't support overloading on const&& for member functions
52#define TL_OPTIONAL_NO_CONSTRR
53
54// GCC < 5 doesn't support some standard C++11 type traits
55#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
56 std::has_trivial_copy_constructor<T>::value
57#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
58 std::has_trivial_copy_assign<T>::value
59
60// This one will be different for GCC 5.7 if it's ever supported
61#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \
62 std::is_trivially_destructible<T>::value
63
64// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks
65// std::vector for non-copyable types
66#elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__))
67#ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
68#define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
69namespace tl {
70namespace detail {
71template<class T>
72struct is_trivially_copy_constructible : std::is_trivially_copy_constructible<T>
73{};
74#ifdef _GLIBCXX_VECTOR
75template<class T, class A>
76struct is_trivially_copy_constructible<std::vector<T, A>>
77 : std::is_trivially_copy_constructible<T>
78{};
79#endif
80}
81}
82#endif
83
84#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
85 tl::detail::is_trivially_copy_constructible<T>::value
86#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
87 std::is_trivially_copy_assignable<T>::value
88#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \
89 std::is_trivially_destructible<T>::value
90#else
91#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
92 std::is_trivially_copy_constructible<T>::value
93#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
94 std::is_trivially_copy_assignable<T>::value
95#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \
96 std::is_trivially_destructible<T>::value
97#endif
98
99#if __cplusplus > 201103L
100#define TL_OPTIONAL_CXX14
101#endif
102
103// constexpr implies const in C++11, not C++14
104#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || \
105 defined(TL_OPTIONAL_GCC49))
106#define TL_OPTIONAL_11_CONSTEXPR
107#else
108#define TL_OPTIONAL_11_CONSTEXPR constexpr
109#endif
110
111namespace tl {
112#ifndef TL_MONOSTATE_INPLACE_MUTEX
113#define TL_MONOSTATE_INPLACE_MUTEX
116{};
117
120{
121 explicit in_place_t() = default;
122};
123
124static constexpr in_place_t in_place{};
125#endif
126
127template<class T>
128class optional;
129
130namespace detail {
131#ifndef TL_TRAITS_MUTEX
132#define TL_TRAITS_MUTEX
133// C++14-style aliases for brevity
134template<class T>
135using remove_const_t = typename std::remove_const<T>::type;
136template<class T>
137using remove_reference_t = typename std::remove_reference<T>::type;
138template<class T>
139using decay_t = typename std::decay<T>::type;
140template<bool E, class T = void>
141using enable_if_t = typename std::enable_if<E, T>::type;
142template<bool B, class T, class F>
143using conditional_t = typename std::conditional<B, T, F>::type;
144
145// std::conjunction from C++17
146template<class...>
147struct conjunction : std::true_type
148{};
149template<class B>
151{};
152template<class B, class... Bs>
153struct conjunction<B, Bs...>
154 : std::conditional<bool(B::value), conjunction<Bs...>, B>::type
155{};
156
157#if defined(_LIBCPP_VERSION) && __cplusplus == 201103L
158#define TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND
159#endif
160
161// In C++11 mode, there's an issue in libc++'s std::mem_fn
162// which results in a hard-error when using it in a noexcept expression
163// in some cases. This is a check to workaround the common failing case.
164#ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND
165template<class T>
166struct is_pointer_to_non_const_member_func : std::false_type
167{};
168template<class T, class Ret, class... Args>
169struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)> : std::true_type
170{};
171template<class T, class Ret, class... Args>
172struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &>
173 : std::true_type
174{};
175template<class T, class Ret, class... Args>
176struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &&>
177 : std::true_type
178{};
179template<class T, class Ret, class... Args>
180struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile>
181 : std::true_type
182{};
183template<class T, class Ret, class... Args>
184struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&>
185 : std::true_type
186{};
187template<class T, class Ret, class... Args>
188struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile&&>
189 : std::true_type
190{};
191
192template<class T>
193struct is_const_or_const_ref : std::false_type
194{};
195template<class T>
196struct is_const_or_const_ref<T const&> : std::true_type
197{};
198template<class T>
199struct is_const_or_const_ref<T const> : std::true_type
200{};
201#endif
202
203// std::invoke from C++17
204// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
205template<
206 typename Fn,
207 typename... Args,
208#ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND
209 typename = enable_if_t<!(is_pointer_to_non_const_member_func<Fn>::value &&
210 is_const_or_const_ref<Args...>::value)>,
211#endif
213 int = 0>
214constexpr auto
215invoke(Fn&& f, Args&&... args) noexcept(
216 noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
217 -> decltype(std::mem_fn(f)(std::forward<Args>(args)...))
218{
219 return std::mem_fn(f)(std::forward<Args>(args)...);
220}
221
222template<typename Fn,
223 typename... Args,
225constexpr auto
226invoke(Fn&& f, Args&&... args) noexcept(
227 noexcept(std::forward<Fn>(f)(std::forward<Args>(args)...)))
228 -> decltype(std::forward<Fn>(f)(std::forward<Args>(args)...))
229{
230 return std::forward<Fn>(f)(std::forward<Args>(args)...);
231}
232
233// std::invoke_result from C++17
234template<class F, class, class... Us>
236
237template<class F, class... Us>
239 F,
240 decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...), void()),
241 Us...>
242{
243 using type =
244 decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...));
245};
246
247template<class F, class... Us>
248using invoke_result = invoke_result_impl<F, void, Us...>;
249
250template<class F, class... Us>
251using invoke_result_t = typename invoke_result<F, Us...>::type;
252
253#if defined(_MSC_VER) && _MSC_VER <= 1900
254// TODO make a version which works with MSVC 2015
255template<class T, class U = T>
256struct is_swappable : std::true_type
257{};
258
259template<class T, class U = T>
260struct is_nothrow_swappable : std::true_type
261{};
262#else
263// https://stackoverflow.com/questions/26744589/what-is-a-proper-way-to-implement-is-swappable-to-test-for-the-swappable-concept
264namespace swap_adl_tests {
265// if swap ADL finds this then it would call std::swap otherwise (same
266// signature)
267struct tag
268{};
269
270template<class T>
271tag
272swap(T&, T&);
273template<class T, std::size_t N>
274tag swap(T (&a)[N], T (&b)[N]);
275
276// helper functions to test if an unqualified swap is possible, and if it
277// becomes std::swap
278template<class, class>
279std::false_type
280can_swap(...) noexcept(false);
281template<class T,
282 class U,
283 class = decltype(swap(std::declval<T&>(), std::declval<U&>()))>
284std::true_type
285can_swap(int) noexcept(noexcept(swap(std::declval<T&>(), std::declval<U&>())));
286
287template<class, class>
288std::false_type
290template<class T, class U>
291std::is_same<decltype(swap(std::declval<T&>(), std::declval<U&>())), tag>
293
294template<class T>
296 : std::integral_constant<bool,
297 std::is_nothrow_move_constructible<T>::value &&
298 std::is_nothrow_move_assignable<T>::value>
299{};
300
301template<class T, std::size_t N>
304
305template<class T, class U>
307 : std::integral_constant<bool, noexcept(can_swap<T, U>(0))>
308{};
309} // namespace swap_adl_tests
310
311template<class T, class U = T>
313 : std::integral_constant<
314 bool,
315 decltype(detail::swap_adl_tests::can_swap<T, U>(0))::value &&
316 (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value ||
317 (std::is_move_assignable<T>::value &&
318 std::is_move_constructible<T>::value))>
319{};
320
321template<class T, std::size_t N>
322struct is_swappable<T[N], T[N]>
323 : std::integral_constant<
324 bool,
325 decltype(detail::swap_adl_tests::can_swap<T[N], T[N]>(0))::value &&
326 (!decltype(detail::swap_adl_tests::uses_std<T[N], T[N]>(0))::value ||
327 is_swappable<T, T>::value)>
328{};
329
330template<class T, class U = T>
332 : std::integral_constant<
333 bool,
334 is_swappable<T, U>::value &&
335 ((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value &&
336 detail::swap_adl_tests::is_std_swap_noexcept<T>::value) ||
337 (!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value &&
338 detail::swap_adl_tests::is_adl_swap_noexcept<T, U>::value))>
339{};
340#endif
341#endif
342
343// std::void_t from C++17
344template<class...>
345struct voider
346{
347 using type = void;
348};
349template<class... Ts>
350using void_t = typename voider<Ts...>::type;
351
352// Trait for checking if a type is a tl::optional
353template<class T>
354struct is_optional_impl : std::false_type
355{};
356template<class T>
357struct is_optional_impl<optional<T>> : std::true_type
358{};
359template<class T>
361
362// Change void to tl::monostate
363template<class U>
365
366template<class F, class U, class = invoke_result_t<F, U>>
368
369// Check if invoking F for some Us returns void
370template<class F, class = void, class... U>
372template<class F, class... U>
373struct returns_void_impl<F, void_t<invoke_result_t<F, U...>>, U...>
374 : std::is_void<invoke_result_t<F, U...>>
375{};
376template<class F, class... U>
377using returns_void = returns_void_impl<F, void, U...>;
378
379template<class T, class... U>
380using enable_if_ret_void = enable_if_t<returns_void<T&&, U...>::value>;
381
382template<class T, class... U>
383using disable_if_ret_void = enable_if_t<!returns_void<T&&, U...>::value>;
384
385template<class T, class U>
388 !std::is_same<detail::decay_t<U>, in_place_t>::value &&
389 !std::is_same<optional<T>, detail::decay_t<U>>::value>;
390
391template<class T, class U, class Other>
394 !std::is_constructible<T, optional<U>&>::value &&
395 !std::is_constructible<T, optional<U>&&>::value &&
396 !std::is_constructible<T, const optional<U>&>::value &&
397 !std::is_constructible<T, const optional<U>&&>::value &&
398 !std::is_convertible<optional<U>&, T>::value &&
399 !std::is_convertible<optional<U>&&, T>::value &&
400 !std::is_convertible<const optional<U>&, T>::value &&
401 !std::is_convertible<const optional<U>&&, T>::value>;
402
403template<class T, class U>
405 !std::is_same<optional<T>, detail::decay_t<U>>::value &&
407 std::is_same<T, detail::decay_t<U>>>::value &&
408 std::is_constructible<T, U>::value && std::is_assignable<T&, U>::value>;
409
410template<class T, class U, class Other>
413 std::is_assignable<T&, Other>::value &&
414 !std::is_constructible<T, optional<U>&>::value &&
415 !std::is_constructible<T, optional<U>&&>::value &&
416 !std::is_constructible<T, const optional<U>&>::value &&
417 !std::is_constructible<T, const optional<U>&&>::value &&
418 !std::is_convertible<optional<U>&, T>::value &&
419 !std::is_convertible<optional<U>&&, T>::value &&
420 !std::is_convertible<const optional<U>&, T>::value &&
421 !std::is_convertible<const optional<U>&&, T>::value &&
422 !std::is_assignable<T&, optional<U>&>::value &&
423 !std::is_assignable<T&, optional<U>&&>::value &&
424 !std::is_assignable<T&, const optional<U>&>::value &&
425 !std::is_assignable<T&, const optional<U>&&>::value>;
426
427// The storage base manages the actual storage, and correctly propagates
428// trivial destruction from T. This case is for when T is not trivially
429// destructible.
430template<class T, bool = ::std::is_trivially_destructible<T>::value>
432{
434 : m_dummy()
435 , m_has_value(false)
436 {
437 }
438
439 template<class... U>
441 : m_value(std::forward<U>(u)...)
442 , m_has_value(true)
443 {
444 }
445
447 {
448 if (m_has_value) {
449 m_value.~T();
450 m_has_value = false;
451 }
452 }
453
454 struct dummy
455 {};
456 union
457 {
460 };
461
463};
464
465// This case is for when T is trivially destructible.
466template<class T>
467struct optional_storage_base<T, true>
468{
470 : m_dummy()
471 , m_has_value(false)
472 {
473 }
474
475 template<class... U>
477 : m_value(std::forward<U>(u)...)
478 , m_has_value(true)
479 {
480 }
481
482 // No destructor, so this class is trivially destructible
483
484 struct dummy
485 {};
486 union
487 {
490 };
491
492 bool m_has_value = false;
493};
494
495// This base class provides some handy member functions which can be used in
496// further derived classes
497template<class T>
499{
501
502 void hard_reset() noexcept
503 {
504 get().~T();
505 this->m_has_value = false;
506 }
507
508 template<class... Args>
509 void construct(Args&&... args)
510 {
511 new (std::addressof(this->m_value)) T(std::forward<Args>(args)...);
512 this->m_has_value = true;
513 }
514
515 template<class Opt>
516 void assign(Opt&& rhs)
517 {
518 if (this->has_value()) {
519 if (rhs.has_value()) {
520 this->m_value = std::forward<Opt>(rhs).get();
521 } else {
522 this->m_value.~T();
523 this->m_has_value = false;
524 }
525 }
526
527 else if (rhs.has_value()) {
528 construct(std::forward<Opt>(rhs).get());
529 }
530 }
531
532 bool has_value() const { return this->m_has_value; }
533
534 TL_OPTIONAL_11_CONSTEXPR T& get() & { return this->m_value; }
535 TL_OPTIONAL_11_CONSTEXPR const T& get() const& { return this->m_value; }
536 TL_OPTIONAL_11_CONSTEXPR T&& get() && { return std::move(this->m_value); }
537#ifndef TL_OPTIONAL_NO_CONSTRR
538 constexpr const T&& get() const&& { return std::move(this->m_value); }
539#endif
540};
541
542// This class manages conditionally having a trivial copy constructor
543// This specialization is for when T is trivially copy constructible
544template<class T, bool = TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)>
549
550// This specialization is for when T is not trivially copy constructible
551template<class T>
553{
555
556 optional_copy_base() = default;
559 {
560 if (rhs.has_value()) {
561 this->construct(rhs.get());
562 } else {
563 this->m_has_value = false;
564 }
565 }
566
570};
571
572// This class manages conditionally having a trivial move constructor
573// Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it
574// doesn't implement an analogue to std::is_trivially_move_constructible. We
575// have to make do with a non-trivial move constructor even if T is trivially
576// move constructible
577#ifndef TL_OPTIONAL_GCC49
578template<class T, bool = std::is_trivially_move_constructible<T>::value>
583#else
584template<class T, bool = false>
586#endif
587template<class T>
589{
591
592 optional_move_base() = default;
594
596 std::is_nothrow_move_constructible<T>::value)
597 {
598 if (rhs.has_value()) {
599 this->construct(std::move(rhs.get()));
600 } else {
601 this->m_has_value = false;
602 }
603 }
606};
607
608// This class manages conditionally having a trivial copy assignment operator
609template<class T,
617
618template<class T>
635
636// This class manages conditionally having a trivial move assignment operator
637// Unfortunately there's no way to achieve this in GCC < 5 AFAIK, since it
638// doesn't implement an analogue to std::is_trivially_move_assignable. We have
639// to make do with a non-trivial move assignment operator even if T is trivially
640// move assignable
641#ifndef TL_OPTIONAL_GCC49
642template<class T,
643 bool = std::is_trivially_destructible<T>::value &&
644 std::is_trivially_move_constructible<T>::value &&
645 std::is_trivially_move_assignable<T>::value>
650#else
651template<class T, bool = false>
653#endif
654
655template<class T>
657{
659
662
664
666 default;
667
670 std::is_nothrow_move_constructible<T>::value &&
671 std::is_nothrow_move_assignable<T>::value)
672 {
673 this->assign(std::move(rhs));
674 return *this;
675 }
676};
677
678// optional_delete_ctor_base will conditionally delete copy and move
679// constructors depending on whether T is copy/move constructible
680template<class T,
681 bool EnableCopy = std::is_copy_constructible<T>::value,
682 bool EnableMove = std::is_move_constructible<T>::value>
693
694template<class T>
705
706template<class T>
717
718template<class T>
729
730// optional_delete_assign_base will conditionally delete copy and move
731// constructors depending on whether T is copy/move constructible + assignable
732template<class T,
733 bool EnableCopy = (std::is_copy_constructible<T>::value &&
734 std::is_copy_assignable<T>::value),
735 bool EnableMove = (std::is_move_constructible<T>::value &&
736 std::is_move_assignable<T>::value)>
747
748template<class T>
759
760template<class T>
771
772template<class T>
783
784} // namespace detail
785
788{
790 {};
791 constexpr explicit nullopt_t(do_not_use, do_not_use) noexcept {}
792};
793
796
797class bad_optional_access : public std::exception
798{
799public:
801 const char* what() const noexcept { return "Optional has no value"; }
802};
803
810template<class T>
815{
817
818 static_assert(!std::is_same<T, in_place_t>::value,
819 "instantiation of optional with in_place_t is ill-formed");
820 static_assert(!std::is_same<detail::decay_t<T>, nullopt_t>::value,
821 "instantiation of optional with nullopt_t is ill-formed");
822
823public:
824// The different versions for C++14 and 11 are needed because deduced return
825// types are not SFINAE-safe. This provides better support for things like
826// generic lambdas. C.f.
827// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html
828#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
829 !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
832 template<class F>
834 {
835 using result = detail::invoke_result_t<F, T&>;
837 "F must return an optional");
838
839 return has_value() ? detail::invoke(std::forward<F>(f), **this)
840 : result(nullopt);
841 }
842
843 template<class F>
845 {
848 "F must return an optional");
849
850 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
851 : result(nullopt);
852 }
853
854 template<class F>
855 constexpr auto and_then(F&& f) const&
856 {
859 "F must return an optional");
860
861 return has_value() ? detail::invoke(std::forward<F>(f), **this)
862 : result(nullopt);
863 }
864
865#ifndef TL_OPTIONAL_NO_CONSTRR
866 template<class F>
867 constexpr auto and_then(F&& f) const&&
868 {
871 "F must return an optional");
872
873 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
874 : result(nullopt);
875 }
876#endif
877#else
880 template<class F>
882 {
883 using result = detail::invoke_result_t<F, T&>;
885 "F must return an optional");
886
887 return has_value() ? detail::invoke(std::forward<F>(f), **this)
888 : result(nullopt);
889 }
890
891 template<class F>
893 {
896 "F must return an optional");
897
898 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
899 : result(nullopt);
900 }
901
902 template<class F>
904 {
907 "F must return an optional");
908
909 return has_value() ? detail::invoke(std::forward<F>(f), **this)
910 : result(nullopt);
911 }
912
913#ifndef TL_OPTIONAL_NO_CONSTRR
914 template<class F>
916 {
919 "F must return an optional");
920
921 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
922 : result(nullopt);
923 }
924#endif
925#endif
926
927#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
928 !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
930 template<class F>
931 TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) &
932 {
933 return optional_map_impl(*this, std::forward<F>(f));
934 }
935
936 template<class F>
937 TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) &&
938 {
939 return optional_map_impl(std::move(*this), std::forward<F>(f));
940 }
941
942 template<class F>
943 constexpr auto map(F&& f) const&
944 {
945 return optional_map_impl(*this, std::forward<F>(f));
946 }
947
948 template<class F>
949 constexpr auto map(F&& f) const&&
950 {
951 return optional_map_impl(std::move(*this), std::forward<F>(f));
952 }
953#else
955 template<class F>
956 TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&>(),
957 std::declval<F&&>()))
958 map(F&& f) &
959 {
960 return optional_map_impl(*this, std::forward<F>(f));
961 }
962
963 template<class F>
964 TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(
965 std::declval<optional&&>(),
966 std::declval<F&&>()))
967 map(F&& f) &&
968 {
969 return optional_map_impl(std::move(*this), std::forward<F>(f));
970 }
971
972 template<class F>
973 constexpr decltype(optional_map_impl(std::declval<const optional&>(),
974 std::declval<F&&>()))
975 map(F&& f) const&
976 {
977 return optional_map_impl(*this, std::forward<F>(f));
978 }
979
980#ifndef TL_OPTIONAL_NO_CONSTRR
981 template<class F>
982 constexpr decltype(optional_map_impl(std::declval<const optional&&>(),
983 std::declval<F&&>())) map(F&& f) const&&
984 {
985 return optional_map_impl(std::move(*this), std::forward<F>(f));
986 }
987#endif
988#endif
989
990#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
991 !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
993 template<class F>
994 TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) &
995 {
996 return optional_map_impl(*this, std::forward<F>(f));
997 }
998
999 template<class F>
1000 TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) &&
1001 {
1002 return optional_map_impl(std::move(*this), std::forward<F>(f));
1003 }
1004
1005 template<class F>
1006 constexpr auto transform(F&& f) const&
1007 {
1008 return optional_map_impl(*this, std::forward<F>(f));
1009 }
1010
1011 template<class F>
1012 constexpr auto transform(F&& f) const&&
1013 {
1014 return optional_map_impl(std::move(*this), std::forward<F>(f));
1015 }
1016#else
1018 template<class F>
1019 TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval<optional&>(),
1020 std::declval<F&&>()))
1021 transform(F&& f) &
1022 {
1023 return optional_map_impl(*this, std::forward<F>(f));
1024 }
1025
1026 template<class F>
1027 TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(
1028 std::declval<optional&&>(),
1029 std::declval<F&&>()))
1030 transform(F&& f) &&
1031 {
1032 return optional_map_impl(std::move(*this), std::forward<F>(f));
1033 }
1034
1035 template<class F>
1036 constexpr decltype(optional_map_impl(std::declval<const optional&>(),
1037 std::declval<F&&>()))
1038 transform(F&& f) const&
1039 {
1040 return optional_map_impl(*this, std::forward<F>(f));
1041 }
1042
1043#ifndef TL_OPTIONAL_NO_CONSTRR
1044 template<class F>
1045 constexpr decltype(optional_map_impl(std::declval<const optional&&>(),
1046 std::declval<F&&>())) transform(F&& f)
1047 const&&
1048 {
1049 return optional_map_impl(std::move(*this), std::forward<F>(f));
1050 }
1051#endif
1052#endif
1053
1055 template<class F, detail::enable_if_ret_void<F>* = nullptr>
1057 {
1058 if (has_value())
1059 return *this;
1060
1061 std::forward<F>(f)();
1062 return nullopt;
1063 }
1064
1065 template<class F, detail::disable_if_ret_void<F>* = nullptr>
1067 {
1068 return has_value() ? *this : std::forward<F>(f)();
1069 }
1070
1071 template<class F, detail::enable_if_ret_void<F>* = nullptr>
1073 {
1074 if (has_value())
1075 return std::move(*this);
1076
1077 std::forward<F>(f)();
1078 return nullopt;
1079 }
1080
1081 template<class F, detail::disable_if_ret_void<F>* = nullptr>
1083 {
1084 return has_value() ? std::move(*this) : std::forward<F>(f)();
1085 }
1086
1087 template<class F, detail::enable_if_ret_void<F>* = nullptr>
1088 optional<T> or_else(F&& f) const&
1089 {
1090 if (has_value())
1091 return *this;
1092
1093 std::forward<F>(f)();
1094 return nullopt;
1095 }
1096
1097 template<class F, detail::disable_if_ret_void<F>* = nullptr>
1099 {
1100 return has_value() ? *this : std::forward<F>(f)();
1101 }
1102
1103#ifndef TL_OPTIONAL_NO_CONSTRR
1104 template<class F, detail::enable_if_ret_void<F>* = nullptr>
1105 optional<T> or_else(F&& f) const&&
1106 {
1107 if (has_value())
1108 return std::move(*this);
1109
1110 std::forward<F>(f)();
1111 return nullopt;
1112 }
1113
1114 template<class F, detail::disable_if_ret_void<F>* = nullptr>
1115 optional<T> or_else(F&& f) const&&
1116 {
1117 return has_value() ? std::move(*this) : std::forward<F>(f)();
1118 }
1119#endif
1120
1122 template<class F, class U>
1123 U map_or(F&& f, U&& u) &
1124 {
1125 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1126 : std::forward<U>(u);
1127 }
1128
1129 template<class F, class U>
1130 U map_or(F&& f, U&& u) &&
1131 {
1132 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
1133 : std::forward<U>(u);
1134 }
1135
1136 template<class F, class U>
1137 U map_or(F&& f, U&& u) const&
1138 {
1139 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1140 : std::forward<U>(u);
1141 }
1142
1143#ifndef TL_OPTIONAL_NO_CONSTRR
1144 template<class F, class U>
1145 U map_or(F&& f, U&& u) const&&
1146 {
1147 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
1148 : std::forward<U>(u);
1149 }
1150#endif
1151
1154 template<class F, class U>
1156 {
1157 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1158 : std::forward<U>(u)();
1159 }
1160
1161 template<class F, class U>
1163 {
1164 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
1165 : std::forward<U>(u)();
1166 }
1167
1168 template<class F, class U>
1170 {
1171 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1172 : std::forward<U>(u)();
1173 }
1174
1175#ifndef TL_OPTIONAL_NO_CONSTRR
1176 template<class F, class U>
1178 {
1179 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
1180 : std::forward<U>(u)();
1181 }
1182#endif
1183
1185 template<class U>
1187 {
1188 using result = optional<detail::decay_t<U>>;
1189 return has_value() ? result{ u } : result{ nullopt };
1190 }
1191
1194 {
1195 return has_value() ? *this : rhs;
1196 }
1197
1198 constexpr optional disjunction(const optional& rhs) const&
1199 {
1200 return has_value() ? *this : rhs;
1201 }
1202
1204 {
1205 return has_value() ? std::move(*this) : rhs;
1206 }
1207
1208#ifndef TL_OPTIONAL_NO_CONSTRR
1209 constexpr optional disjunction(const optional& rhs) const&&
1210 {
1211 return has_value() ? std::move(*this) : rhs;
1212 }
1213#endif
1214
1216 {
1217 return has_value() ? *this : std::move(rhs);
1218 }
1219
1220 constexpr optional disjunction(optional&& rhs) const&
1221 {
1222 return has_value() ? *this : std::move(rhs);
1223 }
1224
1226 {
1227 return has_value() ? std::move(*this) : std::move(rhs);
1228 }
1229
1230#ifndef TL_OPTIONAL_NO_CONSTRR
1231 constexpr optional disjunction(optional&& rhs) const&&
1232 {
1233 return has_value() ? std::move(*this) : std::move(rhs);
1234 }
1235#endif
1236
1239 {
1240 optional ret = std::move(*this);
1241 reset();
1242 return ret;
1243 }
1244
1245 using value_type = T;
1246
1248 constexpr optional() noexcept = default;
1249
1250 constexpr optional(nullopt_t) noexcept {}
1251
1257
1263
1265 template<class... Args>
1266 constexpr explicit optional(
1267 detail::enable_if_t<std::is_constructible<T, Args...>::value, in_place_t>,
1268 Args&&... args)
1269 : base(in_place, std::forward<Args>(args)...)
1270 {
1271 }
1272
1273 template<class U, class... Args>
1276 std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
1277 in_place_t>,
1278 std::initializer_list<U> il,
1279 Args&&... args)
1280 {
1281 this->construct(il, std::forward<Args>(args)...);
1282 }
1283
1285 template<class U = T,
1288 constexpr optional(U&& u)
1289 : base(in_place, std::forward<U>(u))
1290 {
1291 }
1292
1293 template<class U = T,
1296 constexpr explicit optional(U&& u)
1297 : base(in_place, std::forward<U>(u))
1298 {
1299 }
1300
1302 template<
1303 class U,
1307 {
1308 if (rhs.has_value()) {
1309 this->construct(*rhs);
1310 }
1311 }
1312
1313 template<
1314 class U,
1317 explicit optional(const optional<U>& rhs)
1318 {
1319 if (rhs.has_value()) {
1320 this->construct(*rhs);
1321 }
1322 }
1323
1325 template<class U,
1329 {
1330 if (rhs.has_value()) {
1331 this->construct(std::move(*rhs));
1332 }
1333 }
1334
1335 template<class U,
1338 explicit optional(optional<U>&& rhs)
1339 {
1340 if (rhs.has_value()) {
1341 this->construct(std::move(*rhs));
1342 }
1343 }
1344
1346 ~optional() = default;
1347
1352 {
1353 if (has_value()) {
1354 this->m_value.~T();
1355 this->m_has_value = false;
1356 }
1357
1358 return *this;
1359 }
1360
1365 optional& operator=(const optional& rhs) = default;
1366
1371 optional& operator=(optional&& rhs) = default;
1372
1375 template<class U = T, detail::enable_assign_forward<T, U>* = nullptr>
1377 {
1378 if (has_value()) {
1379 this->m_value = std::forward<U>(u);
1380 } else {
1381 this->construct(std::forward<U>(u));
1382 }
1383
1384 return *this;
1385 }
1386
1391 template<class U, detail::enable_assign_from_other<T, U, const U&>* = nullptr>
1393 {
1394 if (has_value()) {
1395 if (rhs.has_value()) {
1396 this->m_value = *rhs;
1397 } else {
1398 this->hard_reset();
1399 }
1400 }
1401
1402 else if (rhs.has_value()) {
1403 this->construct(*rhs);
1404 }
1405
1406 return *this;
1407 }
1408
1409 // TODO check exception guarantee
1414 template<class U, detail::enable_assign_from_other<T, U, U>* = nullptr>
1416 {
1417 if (has_value()) {
1418 if (rhs.has_value()) {
1419 this->m_value = std::move(*rhs);
1420 } else {
1421 this->hard_reset();
1422 }
1423 }
1424
1425 else if (rhs.has_value()) {
1426 this->construct(std::move(*rhs));
1427 }
1428
1429 return *this;
1430 }
1431
1434 template<class... Args>
1435 T& emplace(Args&&... args)
1436 {
1437 static_assert(std::is_constructible<T, Args&&...>::value,
1438 "T must be constructible with Args");
1439
1440 *this = nullopt;
1441 this->construct(std::forward<Args>(args)...);
1442 return value();
1443 }
1444
1445 template<class U, class... Args>
1447 std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value,
1448 T&>
1449 emplace(std::initializer_list<U> il, Args&&... args)
1450 {
1451 *this = nullopt;
1452 this->construct(il, std::forward<Args>(args)...);
1453 return value();
1454 }
1455
1462 void swap(optional& rhs) noexcept(
1463 std::is_nothrow_move_constructible<T>::value &&
1465 {
1466 using std::swap;
1467 if (has_value()) {
1468 if (rhs.has_value()) {
1469 swap(**this, *rhs);
1470 } else {
1471 new (std::addressof(rhs.m_value)) T(std::move(this->m_value));
1472 this->m_value.T::~T();
1473 }
1474 } else if (rhs.has_value()) {
1475 new (std::addressof(this->m_value)) T(std::move(rhs.m_value));
1476 rhs.m_value.T::~T();
1477 }
1478 swap(this->m_has_value, rhs.m_has_value);
1479 }
1480
1482 constexpr const T* operator->() const
1483 {
1484 return std::addressof(this->m_value);
1485 }
1486
1488 {
1489 return std::addressof(this->m_value);
1490 }
1491
1494
1495 constexpr const T& operator*() const& { return this->m_value; }
1496
1498 {
1499 return std::move(this->m_value);
1500 }
1501
1502#ifndef TL_OPTIONAL_NO_CONSTRR
1503 constexpr const T&& operator*() const&& { return std::move(this->m_value); }
1504#endif
1505
1507 constexpr bool has_value() const noexcept { return this->m_has_value; }
1508
1509 constexpr explicit operator bool() const noexcept
1510 {
1511 return this->m_has_value;
1512 }
1513
1517 {
1518 if (has_value())
1519 return this->m_value;
1520 throw bad_optional_access();
1521 }
1523 {
1524 if (has_value())
1525 return this->m_value;
1526 throw bad_optional_access();
1527 }
1529 {
1530 if (has_value())
1531 return std::move(this->m_value);
1532 throw bad_optional_access();
1533 }
1534
1535#ifndef TL_OPTIONAL_NO_CONSTRR
1536 TL_OPTIONAL_11_CONSTEXPR const T&& value() const&&
1537 {
1538 if (has_value())
1539 return std::move(this->m_value);
1540 throw bad_optional_access();
1541 }
1542#endif
1543
1545 template<class U>
1546 constexpr T value_or(U&& u) const&
1547 {
1548 static_assert(std::is_copy_constructible<T>::value &&
1549 std::is_convertible<U&&, T>::value,
1550 "T must be copy constructible and convertible from U");
1551 return has_value() ? **this : static_cast<T>(std::forward<U>(u));
1552 }
1553
1554 template<class U>
1556 {
1557 static_assert(std::is_move_constructible<T>::value &&
1558 std::is_convertible<U&&, T>::value,
1559 "T must be move constructible and convertible from U");
1560 return has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(u));
1561 }
1562
1564 void reset() noexcept
1565 {
1566 if (has_value()) {
1567 this->m_value.~T();
1568 this->m_has_value = false;
1569 }
1570 }
1571}; // namespace tl
1572
1574template<class T, class U>
1575inline constexpr bool
1576operator==(const optional<T>& lhs, const optional<U>& rhs)
1577{
1578 return lhs.has_value() == rhs.has_value() &&
1579 (!lhs.has_value() || *lhs == *rhs);
1580}
1581template<class T, class U>
1582inline constexpr bool
1583operator!=(const optional<T>& lhs, const optional<U>& rhs)
1584{
1585 return lhs.has_value() != rhs.has_value() ||
1586 (lhs.has_value() && *lhs != *rhs);
1587}
1588template<class T, class U>
1589inline constexpr bool
1590operator<(const optional<T>& lhs, const optional<U>& rhs)
1591{
1592 return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs);
1593}
1594template<class T, class U>
1595inline constexpr bool
1596operator>(const optional<T>& lhs, const optional<U>& rhs)
1597{
1598 return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs);
1599}
1600template<class T, class U>
1601inline constexpr bool
1602operator<=(const optional<T>& lhs, const optional<U>& rhs)
1603{
1604 return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs);
1605}
1606template<class T, class U>
1607inline constexpr bool
1608operator>=(const optional<T>& lhs, const optional<U>& rhs)
1609{
1610 return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs);
1611}
1612
1614template<class T>
1615inline constexpr bool
1616operator==(const optional<T>& lhs, nullopt_t) noexcept
1617{
1618 return !lhs.has_value();
1619}
1620template<class T>
1621inline constexpr bool
1622operator==(nullopt_t, const optional<T>& rhs) noexcept
1623{
1624 return !rhs.has_value();
1625}
1626template<class T>
1627inline constexpr bool
1628operator!=(const optional<T>& lhs, nullopt_t) noexcept
1629{
1630 return lhs.has_value();
1631}
1632template<class T>
1633inline constexpr bool
1634operator!=(nullopt_t, const optional<T>& rhs) noexcept
1635{
1636 return rhs.has_value();
1637}
1638template<class T>
1639inline constexpr bool
1640operator<(const optional<T>&, nullopt_t) noexcept
1641{
1642 return false;
1643}
1644template<class T>
1645inline constexpr bool
1646operator<(nullopt_t, const optional<T>& rhs) noexcept
1647{
1648 return rhs.has_value();
1649}
1650template<class T>
1651inline constexpr bool
1652operator<=(const optional<T>& lhs, nullopt_t) noexcept
1653{
1654 return !lhs.has_value();
1655}
1656template<class T>
1657inline constexpr bool
1658operator<=(nullopt_t, const optional<T>&) noexcept
1659{
1660 return true;
1661}
1662template<class T>
1663inline constexpr bool
1664operator>(const optional<T>& lhs, nullopt_t) noexcept
1665{
1666 return lhs.has_value();
1667}
1668template<class T>
1669inline constexpr bool
1671{
1672 return false;
1673}
1674template<class T>
1675inline constexpr bool
1677{
1678 return true;
1679}
1680template<class T>
1681inline constexpr bool
1682operator>=(nullopt_t, const optional<T>& rhs) noexcept
1683{
1684 return !rhs.has_value();
1685}
1686
1688template<class T, class U>
1689inline constexpr bool
1690operator==(const optional<T>& lhs, const U& rhs)
1691{
1692 return lhs.has_value() ? *lhs == rhs : false;
1693}
1694template<class T, class U>
1695inline constexpr bool
1696operator==(const U& lhs, const optional<T>& rhs)
1697{
1698 return rhs.has_value() ? lhs == *rhs : false;
1699}
1700template<class T, class U>
1701inline constexpr bool
1702operator!=(const optional<T>& lhs, const U& rhs)
1703{
1704 return lhs.has_value() ? *lhs != rhs : true;
1705}
1706template<class T, class U>
1707inline constexpr bool
1708operator!=(const U& lhs, const optional<T>& rhs)
1709{
1710 return rhs.has_value() ? lhs != *rhs : true;
1711}
1712template<class T, class U>
1713inline constexpr bool
1714operator<(const optional<T>& lhs, const U& rhs)
1715{
1716 return lhs.has_value() ? *lhs < rhs : true;
1717}
1718template<class T, class U>
1719inline constexpr bool
1720operator<(const U& lhs, const optional<T>& rhs)
1721{
1722 return rhs.has_value() ? lhs < *rhs : false;
1723}
1724template<class T, class U>
1725inline constexpr bool
1726operator<=(const optional<T>& lhs, const U& rhs)
1727{
1728 return lhs.has_value() ? *lhs <= rhs : true;
1729}
1730template<class T, class U>
1731inline constexpr bool
1732operator<=(const U& lhs, const optional<T>& rhs)
1733{
1734 return rhs.has_value() ? lhs <= *rhs : false;
1735}
1736template<class T, class U>
1737inline constexpr bool
1738operator>(const optional<T>& lhs, const U& rhs)
1739{
1740 return lhs.has_value() ? *lhs > rhs : false;
1741}
1742template<class T, class U>
1743inline constexpr bool
1744operator>(const U& lhs, const optional<T>& rhs)
1745{
1746 return rhs.has_value() ? lhs > *rhs : true;
1747}
1748template<class T, class U>
1749inline constexpr bool
1750operator>=(const optional<T>& lhs, const U& rhs)
1751{
1752 return lhs.has_value() ? *lhs >= rhs : false;
1753}
1754template<class T, class U>
1755inline constexpr bool
1756operator>=(const U& lhs, const optional<T>& rhs)
1757{
1758 return rhs.has_value() ? lhs >= *rhs : true;
1759}
1760
1761template<class T,
1762 detail::enable_if_t<std::is_move_constructible<T>::value>* = nullptr,
1763 detail::enable_if_t<detail::is_swappable<T>::value>* = nullptr>
1764void
1765swap(optional<T>& lhs, optional<T>& rhs) noexcept(noexcept(lhs.swap(rhs)))
1766{
1767 return lhs.swap(rhs);
1768}
1769
1770namespace detail {
1772{};
1773} // namespace detail
1774
1775template<
1776 class T = detail::i_am_secret,
1777 class U,
1780 T>>
1781inline constexpr optional<Ret>
1783{
1784 return optional<Ret>(std::forward<U>(v));
1785}
1786
1787template<class T, class... Args>
1788inline constexpr optional<T>
1789make_optional(Args&&... args)
1790{
1791 return optional<T>(in_place, std::forward<Args>(args)...);
1792}
1793template<class T, class U, class... Args>
1794inline constexpr optional<T>
1795make_optional(std::initializer_list<U> il, Args&&... args)
1796{
1797 return optional<T>(in_place, il, std::forward<Args>(args)...);
1798}
1799
1800#if __cplusplus >= 201703L
1801template<class T>
1802optional(T) -> optional<T>;
1803#endif
1804
1806namespace detail {
1807#ifdef TL_OPTIONAL_CXX14
1808template<class Opt,
1809 class F,
1810 class Ret = decltype(detail::invoke(std::declval<F>(),
1811 *std::declval<Opt>())),
1812 detail::enable_if_t<!std::is_void<Ret>::value>* = nullptr>
1813constexpr auto
1814optional_map_impl(Opt&& opt, F&& f)
1815{
1816 return opt.has_value()
1817 ? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt))
1818 : optional<Ret>(nullopt);
1819}
1820
1821template<class Opt,
1822 class F,
1823 class Ret = decltype(detail::invoke(std::declval<F>(),
1824 *std::declval<Opt>())),
1825 detail::enable_if_t<std::is_void<Ret>::value>* = nullptr>
1826auto
1827optional_map_impl(Opt&& opt, F&& f)
1828{
1829 if (opt.has_value()) {
1830 detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
1831 return make_optional(monostate{});
1832 }
1833
1834 return optional<monostate>(nullopt);
1835}
1836#else
1837template<class Opt,
1838 class F,
1839 class Ret = decltype(detail::invoke(std::declval<F>(),
1840 *std::declval<Opt>())),
1841 detail::enable_if_t<!std::is_void<Ret>::value>* = nullptr>
1842
1843constexpr auto
1845{
1846 return opt.has_value()
1847 ? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt))
1849}
1850
1851template<class Opt,
1852 class F,
1853 class Ret = decltype(detail::invoke(std::declval<F>(),
1854 *std::declval<Opt>())),
1855 detail::enable_if_t<std::is_void<Ret>::value>* = nullptr>
1856
1857auto
1859{
1860 if (opt.has_value()) {
1861 detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
1862 return monostate{};
1863 }
1864
1865 return nullopt;
1866}
1867#endif
1868} // namespace detail
1869
1872template<class T>
1874{
1875public:
1876// The different versions for C++14 and 11 are needed because deduced return
1877// types are not SFINAE-safe. This provides better support for things like
1878// generic lambdas. C.f.
1879// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html
1880#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
1881 !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
1882
1885 template<class F>
1887 {
1888 using result = detail::invoke_result_t<F, T&>;
1890 "F must return an optional");
1891
1892 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1893 : result(nullopt);
1894 }
1895
1896 template<class F>
1898 {
1899 using result = detail::invoke_result_t<F, T&>;
1901 "F must return an optional");
1902
1903 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1904 : result(nullopt);
1905 }
1906
1907 template<class F>
1908 constexpr auto and_then(F&& f) const&
1909 {
1912 "F must return an optional");
1913
1914 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1915 : result(nullopt);
1916 }
1917
1918#ifndef TL_OPTIONAL_NO_CONSTRR
1919 template<class F>
1920 constexpr auto and_then(F&& f) const&&
1921 {
1924 "F must return an optional");
1925
1926 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1927 : result(nullopt);
1928 }
1929#endif
1930#else
1933 template<class F>
1935 {
1936 using result = detail::invoke_result_t<F, T&>;
1938 "F must return an optional");
1939
1940 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1941 : result(nullopt);
1942 }
1943
1944 template<class F>
1946 {
1947 using result = detail::invoke_result_t<F, T&>;
1949 "F must return an optional");
1950
1951 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1952 : result(nullopt);
1953 }
1954
1955 template<class F>
1957 {
1960 "F must return an optional");
1961
1962 return has_value() ? detail::invoke(std::forward<F>(f), **this)
1963 : result(nullopt);
1964 }
1965
1966#ifndef TL_OPTIONAL_NO_CONSTRR
1967 template<class F>
1969 {
1972 "F must return an optional");
1973
1974 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
1975 : result(nullopt);
1976 }
1977#endif
1978#endif
1979
1980#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
1981 !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
1983 template<class F>
1984 TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) &
1985 {
1986 return detail::optional_map_impl(*this, std::forward<F>(f));
1987 }
1988
1989 template<class F>
1990 TL_OPTIONAL_11_CONSTEXPR auto map(F&& f) &&
1991 {
1992 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
1993 }
1994
1995 template<class F>
1996 constexpr auto map(F&& f) const&
1997 {
1998 return detail::optional_map_impl(*this, std::forward<F>(f));
1999 }
2000
2001 template<class F>
2002 constexpr auto map(F&& f) const&&
2003 {
2004 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2005 }
2006#else
2008 template<class F>
2010 std::declval<optional&>(),
2011 std::declval<F&&>()))
2012 map(F&& f) &
2013 {
2014 return detail::optional_map_impl(*this, std::forward<F>(f));
2015 }
2016
2017 template<class F>
2019 std::declval<optional&&>(),
2020 std::declval<F&&>()))
2021 map(F&& f) &&
2022 {
2023 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2024 }
2025
2026 template<class F>
2027 constexpr decltype(detail::optional_map_impl(std::declval<const optional&>(),
2028 std::declval<F&&>()))
2029 map(F&& f) const&
2030 {
2031 return detail::optional_map_impl(*this, std::forward<F>(f));
2032 }
2033
2034#ifndef TL_OPTIONAL_NO_CONSTRR
2035 template<class F>
2036 constexpr decltype(detail::optional_map_impl(std::declval<const optional&&>(),
2037 std::declval<F&&>())) map(F&& f)
2038 const&&
2039 {
2040 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2041 }
2042#endif
2043#endif
2044
2045#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && \
2046 !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55)
2048 template<class F>
2049 TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) &
2050 {
2051 return detail::optional_map_impl(*this, std::forward<F>(f));
2052 }
2053
2054 template<class F>
2055 TL_OPTIONAL_11_CONSTEXPR auto transform(F&& f) &&
2056 {
2057 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2058 }
2059
2060 template<class F>
2061 constexpr auto transform(F&& f) const&
2062 {
2063 return detail::optional_map_impl(*this, std::forward<F>(f));
2064 }
2065
2066 template<class F>
2067 constexpr auto transform(F&& f) const&&
2068 {
2069 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2070 }
2071#else
2073 template<class F>
2075 std::declval<optional&>(),
2076 std::declval<F&&>()))
2077 transform(F&& f) &
2078 {
2079 return detail::optional_map_impl(*this, std::forward<F>(f));
2080 }
2081
2084 template<class F>
2086 std::declval<optional&&>(),
2087 std::declval<F&&>()))
2088 transform(F&& f) &&
2089 {
2090 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2091 }
2092
2093 template<class F>
2094 constexpr decltype(detail::optional_map_impl(std::declval<const optional&>(),
2095 std::declval<F&&>()))
2096 transform(F&& f) const&
2097 {
2098 return detail::optional_map_impl(*this, std::forward<F>(f));
2099 }
2100
2101#ifndef TL_OPTIONAL_NO_CONSTRR
2102 template<class F>
2103 constexpr decltype(detail::optional_map_impl(
2104 std::declval<const optional&&>(),
2105 std::declval<F&&>())) transform(F&& f) const&&
2106 {
2107 return detail::optional_map_impl(std::move(*this), std::forward<F>(f));
2108 }
2109#endif
2110#endif
2111
2113 template<class F, detail::enable_if_ret_void<F>* = nullptr>
2115 {
2116 if (has_value())
2117 return *this;
2118
2119 std::forward<F>(f)();
2120 return nullopt;
2121 }
2122
2123 template<class F, detail::disable_if_ret_void<F>* = nullptr>
2125 {
2126 return has_value() ? *this : std::forward<F>(f)();
2127 }
2128
2129 template<class F, detail::enable_if_ret_void<F>* = nullptr>
2131 {
2132 if (has_value())
2133 return std::move(*this);
2134
2135 std::forward<F>(f)();
2136 return nullopt;
2137 }
2138
2139 template<class F, detail::disable_if_ret_void<F>* = nullptr>
2141 {
2142 return has_value() ? std::move(*this) : std::forward<F>(f)();
2143 }
2144
2145 template<class F, detail::enable_if_ret_void<F>* = nullptr>
2146 optional<T> or_else(F&& f) const&
2147 {
2148 if (has_value())
2149 return *this;
2150
2151 std::forward<F>(f)();
2152 return nullopt;
2153 }
2154
2155 template<class F, detail::disable_if_ret_void<F>* = nullptr>
2157 {
2158 return has_value() ? *this : std::forward<F>(f)();
2159 }
2160
2161#ifndef TL_OPTIONAL_NO_CONSTRR
2162 template<class F, detail::enable_if_ret_void<F>* = nullptr>
2163 optional<T> or_else(F&& f) const&&
2164 {
2165 if (has_value())
2166 return std::move(*this);
2167
2168 std::forward<F>(f)();
2169 return nullopt;
2170 }
2171
2172 template<class F, detail::disable_if_ret_void<F>* = nullptr>
2173 optional<T> or_else(F&& f) const&&
2174 {
2175 return has_value() ? std::move(*this) : std::forward<F>(f)();
2176 }
2177#endif
2178
2180 template<class F, class U>
2181 U map_or(F&& f, U&& u) &
2182 {
2183 return has_value() ? detail::invoke(std::forward<F>(f), **this)
2184 : std::forward<U>(u);
2185 }
2186
2187 template<class F, class U>
2188 U map_or(F&& f, U&& u) &&
2189 {
2190 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
2191 : std::forward<U>(u);
2192 }
2193
2194 template<class F, class U>
2195 U map_or(F&& f, U&& u) const&
2196 {
2197 return has_value() ? detail::invoke(std::forward<F>(f), **this)
2198 : std::forward<U>(u);
2199 }
2200
2201#ifndef TL_OPTIONAL_NO_CONSTRR
2202 template<class F, class U>
2203 U map_or(F&& f, U&& u) const&&
2204 {
2205 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
2206 : std::forward<U>(u);
2207 }
2208#endif
2209
2212 template<class F, class U>
2214 {
2215 return has_value() ? detail::invoke(std::forward<F>(f), **this)
2216 : std::forward<U>(u)();
2217 }
2218
2219 template<class F, class U>
2221 {
2222 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
2223 : std::forward<U>(u)();
2224 }
2225
2226 template<class F, class U>
2228 {
2229 return has_value() ? detail::invoke(std::forward<F>(f), **this)
2230 : std::forward<U>(u)();
2231 }
2232
2233#ifndef TL_OPTIONAL_NO_CONSTRR
2234 template<class F, class U>
2236 {
2237 return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
2238 : std::forward<U>(u)();
2239 }
2240#endif
2241
2243 template<class U>
2245 {
2246 using result = optional<detail::decay_t<U>>;
2247 return has_value() ? result{ u } : result{ nullopt };
2248 }
2249
2252 {
2253 return has_value() ? *this : rhs;
2254 }
2255
2256 constexpr optional disjunction(const optional& rhs) const&
2257 {
2258 return has_value() ? *this : rhs;
2259 }
2260
2262 {
2263 return has_value() ? std::move(*this) : rhs;
2264 }
2265
2266#ifndef TL_OPTIONAL_NO_CONSTRR
2267 constexpr optional disjunction(const optional& rhs) const&&
2268 {
2269 return has_value() ? std::move(*this) : rhs;
2270 }
2271#endif
2272
2274 {
2275 return has_value() ? *this : std::move(rhs);
2276 }
2277
2278 constexpr optional disjunction(optional&& rhs) const&
2279 {
2280 return has_value() ? *this : std::move(rhs);
2281 }
2282
2284 {
2285 return has_value() ? std::move(*this) : std::move(rhs);
2286 }
2287
2288#ifndef TL_OPTIONAL_NO_CONSTRR
2289 constexpr optional disjunction(optional&& rhs) const&&
2290 {
2291 return has_value() ? std::move(*this) : std::move(rhs);
2292 }
2293#endif
2294
2297 {
2298 optional ret = std::move(*this);
2299 reset();
2300 return ret;
2301 }
2302
2303 using value_type = T&;
2304
2306 constexpr optional() noexcept
2307 : m_value(nullptr)
2308 {
2309 }
2310
2311 constexpr optional(nullopt_t) noexcept
2312 : m_value(nullptr)
2313 {
2314 }
2315
2320 TL_OPTIONAL_11_CONSTEXPR optional(const optional& rhs) noexcept = default;
2321
2327
2329 template<class U = T,
2332 constexpr optional(U&& u) noexcept
2333 : m_value(std::addressof(u))
2334 {
2335 static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
2336 }
2337
2338 template<class U>
2339 constexpr explicit optional(const optional<U>& rhs) noexcept
2340 : optional(*rhs)
2341 {
2342 }
2343
2345 ~optional() = default;
2346
2351 {
2352 m_value = nullptr;
2353 return *this;
2354 }
2355
2360 optional& operator=(const optional& rhs) = default;
2361
2363 template<class U = T,
2367 {
2368 static_assert(std::is_lvalue_reference<U>::value, "U must be an lvalue");
2369 m_value = std::addressof(u);
2370 return *this;
2371 }
2372
2377 template<class U>
2378 optional& operator=(const optional<U>& rhs) noexcept
2379 {
2380 m_value = std::addressof(rhs.value());
2381 return *this;
2382 }
2383
2385 template<class U = T,
2387 !detail::is_optional<detail::decay_t<U>>::value>* = nullptr>
2388 optional& emplace(U&& u) noexcept
2389 {
2390 return *this = std::forward<U>(u);
2391 }
2392
2393 void swap(optional& rhs) noexcept { std::swap(m_value, rhs.m_value); }
2394
2396 constexpr const T* operator->() const noexcept { return m_value; }
2397
2398 TL_OPTIONAL_11_CONSTEXPR T* operator->() noexcept { return m_value; }
2399
2401 TL_OPTIONAL_11_CONSTEXPR T& operator*() noexcept { return *m_value; }
2402
2403 constexpr const T& operator*() const noexcept { return *m_value; }
2404
2405 constexpr bool has_value() const noexcept { return m_value != nullptr; }
2406
2407 constexpr explicit operator bool() const noexcept
2408 {
2409 return m_value != nullptr;
2410 }
2411
2415 {
2416 if (has_value())
2417 return *m_value;
2418 throw bad_optional_access();
2419 }
2421 {
2422 if (has_value())
2423 return *m_value;
2424 throw bad_optional_access();
2425 }
2426
2428 template<class U>
2429 constexpr T value_or(U&& u) const& noexcept
2430 {
2431 static_assert(std::is_copy_constructible<T>::value &&
2432 std::is_convertible<U&&, T>::value,
2433 "T must be copy constructible and convertible from U");
2434 return has_value() ? **this : static_cast<T>(std::forward<U>(u));
2435 }
2436
2438 template<class U>
2440 {
2441 static_assert(std::is_move_constructible<T>::value &&
2442 std::is_convertible<U&&, T>::value,
2443 "T must be move constructible and convertible from U");
2444 return has_value() ? **this : static_cast<T>(std::forward<U>(u));
2445 }
2446
2448 void reset() noexcept { m_value = nullptr; }
2449
2450private:
2451 T* m_value;
2452}; // namespace tl
2453
2454} // namespace tl
2455
2456namespace std {
2457// TODO SFINAE
2458template<class T>
2459struct hash<tl::optional<T>>
2460{
2461 ::std::size_t operator()(const tl::optional<T>& o) const
2462 {
2463 if (!o.has_value())
2464 return 0;
2465
2466 return std::hash<tl::detail::remove_const_t<T>>()(*o);
2467 }
2468};
2469} // namespace std
2470
2471#endif
const char * what() const noexcept
Used to represent an optional with no data; essentially a bool.
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) const &&
constexpr optional(const optional< U > &rhs) noexcept
U map_or(F &&f, U &&u) const &
constexpr optional() noexcept
Constructs an optional that does not contain a value.
TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval< optional && >(), std::declval< F && >())) map(F &&f) &&
constexpr detail::invoke_result_t< F, const T & > and_then(F &&f) const &&
constexpr detail::invoke_result_t< F, const T & > and_then(F &&f) const &
~optional()=default
No-op.
constexpr optional disjunction(const optional &rhs) const &&
optional< T > TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) &
Calls f if the optional is empty.
constexpr optional(nullopt_t) noexcept
constexpr decltype(detail::optional_map_impl(std::declval< const optional && >(), std::declval< F && >())) map(F &&f) const &&
constexpr decltype(detail::optional_map_impl(std::declval< const optional & >(), std::declval< F && >())) transform(F &&f) const &
TL_OPTIONAL_11_CONSTEXPR T & operator*() noexcept
Returns the stored value.
optional & operator=(const optional &rhs)=default
TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) &&noexcept
\group value_or
TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t< F, T & > and_then(F &&f) &
TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) noexcept=default
TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval< optional & >(), std::declval< F && >())) transform(F &&f) &
Carries out some operation on the stored object if there is one.
constexpr optional disjunction(optional &&rhs) const &
TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) &&
U map_or(F &&f, U &&u) const &&
constexpr optional< typename std::decay< U >::type > conjunction(U &&u) const
Returns u if *this has a value, otherwise an empty optional.
TL_OPTIONAL_11_CONSTEXPR T * operator->() noexcept
optional< T > or_else(F &&f) const &
TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs)=default
optional & operator=(const optional< U > &rhs) noexcept
optional & operator=(nullopt_t) noexcept
constexpr decltype(detail::optional_map_impl(std::declval< const optional & >(), std::declval< F && >())) map(F &&f) const &
optional< T > TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const &
optional< T > or_else(F &&f) &&
TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval< optional && >(), std::declval< F && >())) transform(F &&f) &&
void reset() noexcept
Destroys the stored value if one exists, making the optional empty.
constexpr decltype(detail::optional_map_impl(std::declval< const optional && >(), std::declval< F && >())) transform(F &&f) const &&
optional & operator=(U &&u)
Rebinds this optional to u.
TL_OPTIONAL_11_CONSTEXPR T & value()
TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) &
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) &&
U map_or(F &&f, U &&u) &&
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) const &
TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) &&
TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval< optional & >(), std::declval< F && >())) map(F &&f) &
Carries out some operation on the stored object if there is one.
constexpr optional(U &&u) noexcept
Constructs the stored value with u.
TL_OPTIONAL_11_CONSTEXPR const T & value() const
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) &
TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) &
Returns rhs if *this is empty, otherwise the current value.
constexpr optional disjunction(optional &&rhs) const &&
constexpr bool has_value() const noexcept
constexpr T value_or(U &&u) const &noexcept
Returns the stored value if there is one, otherwise returns u.
optional take()
Takes the value out of the optional, leaving it empty.
optional< T > TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) &&
U map_or(F &&f, U &&u) &
Maps the stored value with f if there is one, otherwise returns u.
constexpr const T * operator->() const noexcept
Returns a pointer to the stored value.
constexpr const T & operator*() const noexcept
optional & emplace(U &&u) noexcept
Rebinds this optional to u.
void swap(optional &rhs) noexcept
constexpr optional disjunction(const optional &rhs) const &
TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t< F, T & > and_then(F &&f) &&
optional< T > or_else(F &&f) const &&
constexpr optional() noexcept=default
Constructs an optional that does not contain a value.
constexpr optional disjunction(const optional &rhs) const &
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) const &&
constexpr decltype(optional_map_impl(std::declval< const optional & >(), std::declval< F && >())) map(F &&f) const &
fixup_void< invoke_result_t< F, U > > value_type
TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) &&
TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t< F, T & > and_then(F &&f) &
TL_OPTIONAL_11_CONSTEXPR T && value() &&
TL_OPTIONAL_11_CONSTEXPR T & operator*() &
Returns the stored value.
TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs)=default
constexpr decltype(optional_map_impl(std::declval< const optional && >(), std::declval< F && >())) map(F &&f) const &&
TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) &
Returns rhs if *this is empty, otherwise the current value.
T & emplace(Args &&... args)
TL_OPTIONAL_11_CONSTEXPR const T && value() const &&
optional & operator=(optional &&rhs)=default
U map_or(F &&f, U &&u) const &
TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval< optional && >(), std::declval< F && >())) map(F &&f) &&
TL_OPTIONAL_11_CONSTEXPR T & value() &
optional(const optional< U > &rhs)
Converting copy constructor.
constexpr optional disjunction(optional &&rhs) const &
constexpr bool has_value() const noexcept
Returns whether or not the optional has a value.
TL_OPTIONAL_11_CONSTEXPR T * operator->()
constexpr optional(detail::enable_if_t< std::is_constructible< T, Args... >::value, in_place_t >, Args &&... args)
Constructs the stored value in-place using the given arguments.
optional & operator=(U &&u)
optional< T > or_else(F &&f) const &&
TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval< optional && >(), std::declval< F && >())) transform(F &&f) &&
constexpr const T * operator->() const
Returns a pointer to the stored value.
TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval< optional & >(), std::declval< F && >())) transform(F &&f) &
Carries out some operation on the stored object if there is one.
TL_OPTIONAL_11_CONSTEXPR optional(detail::enable_if_t< std::is_constructible< T, std::initializer_list< U > &, Args &&... >::value, in_place_t >, std::initializer_list< U > il, Args &&... args)
constexpr decltype(optional_map_impl(std::declval< const optional & >(), std::declval< F && >())) transform(F &&f) const &
TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs)=default
U map_or(F &&f, U &&u) const &&
void swap(optional &rhs) noexcept(std::is_nothrow_move_constructible< T >::value &&detail::is_nothrow_swappable< T >::value)
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) const &
U map_or(F &&f, U &&u) &&
TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) &&
TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) &
~optional()=default
Destroys the stored value if there is one.
constexpr detail::invoke_result_t< F, const T && > and_then(F &&f) const &&
optional< T > or_else(F &&f) const &
detail::enable_if_t< std::is_constructible< T, std::initializer_list< U > &, Args &&... >::value, T & > emplace(std::initializer_list< U > il, Args &&... args)
constexpr const T & operator*() const &
TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t< F, T && > and_then(F &&f) &&
TL_OPTIONAL_11_CONSTEXPR const T & value() const &
constexpr optional disjunction(optional &&rhs) const &&
constexpr T value_or(U &&u) const &
Returns the stored value if there is one, otherwise returns u.
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) &
optional take()
Takes the value out of the optional, leaving it empty.
optional & operator=(optional< U > &&rhs)
constexpr optional(U &&u)
Constructs the stored value with u.
constexpr optional< typename std::decay< U >::type > conjunction(U &&u) const
Returns u if *this has a value, otherwise an empty optional.
optional & operator=(const optional< U > &rhs)
TL_OPTIONAL_11_CONSTEXPR T && operator*() &&
optional(optional< U > &&rhs)
Converting move constructor.
constexpr detail::invoke_result_t< F, const T & > and_then(F &&f) const &
constexpr const T && operator*() const &&
constexpr optional disjunction(const optional &rhs) const &&
U map_or(F &&f, U &&u) &
Maps the stored value with f if there is one, otherwise returns u.
optional & operator=(const optional &rhs)=default
TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval< optional & >(), std::declval< F && >())) map(F &&f) &
Carries out some operation on the stored object if there is one.
optional & operator=(nullopt_t) noexcept
optional< T > or_else(F &&f) &&
optional< T > TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) &&
detail::invoke_result_t< U > map_or_else(F &&f, U &&u) &&
TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) &&
optional< T > TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) &
Calls f if the optional is empty.
constexpr decltype(optional_map_impl(std::declval< const optional && >(), std::declval< F && >())) transform(F &&f) const &&
optional< T > TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) const &
STL namespace.
std::false_type uses_std(...)
std::false_type can_swap(...) noexcept(false)
enable_if_t<!returns_void< T &&, U... >::value > disable_if_ret_void
is_optional_impl< decay_t< T > > is_optional
detail::enable_if_t< std::is_constructible< T, U && >::value && !std::is_same< detail::decay_t< U >, in_place_t >::value && !std::is_same< optional< T >, detail::decay_t< U > >::value > enable_forward_value
optional< fixup_void< invoke_result_t< F, U > > > get_map_return
typename std::remove_reference< T >::type remove_reference_t
invoke_result_impl< F, void, Us... > invoke_result
detail::enable_if_t< !std::is_same< optional< T >, detail::decay_t< U > >::value && !detail::conjunction< std::is_scalar< T >, std::is_same< T, detail::decay_t< U > > >::value && std::is_constructible< T, U >::value &&std::is_assignable< T &, U >::value > enable_assign_forward
detail::enable_if_t< std::is_constructible< T, Other >::value && std::is_assignable< T &, Other >::value && !std::is_constructible< T, optional< U > & >::value && !std::is_constructible< T, optional< U > && >::value && !std::is_constructible< T, const optional< U > & >::value && !std::is_constructible< T, const optional< U > && >::value && !std::is_convertible< optional< U > &, T >::value && !std::is_convertible< optional< U > &&, T >::value && !std::is_convertible< const optional< U > &, T >::value && !std::is_convertible< const optional< U > &&, T >::value && !std::is_assignable< T &, optional< U > & >::value && !std::is_assignable< T &, optional< U > && >::value && !std::is_assignable< T &, const optional< U > & >::value && !std::is_assignable< T &, const optional< U > && >::value > enable_assign_from_other
typename std::enable_if< E, T >::type enable_if_t
typename std::decay< T >::type decay_t
typename std::remove_const< T >::type remove_const_t
detail::enable_if_t< std::is_constructible< T, Other >::value && !std::is_constructible< T, optional< U > & >::value && !std::is_constructible< T, optional< U > && >::value && !std::is_constructible< T, const optional< U > & >::value && !std::is_constructible< T, const optional< U > && >::value && !std::is_convertible< optional< U > &, T >::value && !std::is_convertible< optional< U > &&, T >::value && !std::is_convertible< const optional< U > &, T >::value && !std::is_convertible< const optional< U > &&, T >::value > enable_from_other
returns_void_impl< F, void, U... > returns_void
constexpr auto invoke(Fn &&f, Args &&... args) noexcept(noexcept(std::mem_fn(f)(std::forward< Args >(args)...))) -> decltype(std::mem_fn(f)(std::forward< Args >(args)...))
constexpr auto optional_map_impl(Opt &&opt, F &&f) -> optional< Ret >
typename std::conditional< B, T, F >::type conditional_t
enable_if_t< returns_void< T &&, U... >::value > enable_if_ret_void
typename voider< Ts... >::type void_t
conditional_t< std::is_void< U >::value, monostate, U > fixup_void
typename invoke_result< F, Us... >::type invoke_result_t
constexpr bool operator==(const optional< T > &lhs, const optional< U > &rhs)
Compares two optional objects.
static constexpr nullopt_t nullopt
Represents an empty optional.
constexpr bool operator!=(const optional< T > &lhs, const optional< U > &rhs)
constexpr bool operator>=(const optional< T > &lhs, const optional< U > &rhs)
constexpr bool operator<(const optional< T > &lhs, const optional< U > &rhs)
static constexpr in_place_t in_place
A tag to tell optional to construct its value in-place.
constexpr optional< Ret > make_optional(U &&v)
constexpr bool operator>(const optional< T > &lhs, const optional< U > &rhs)
constexpr bool operator<=(const optional< T > &lhs, const optional< U > &rhs)
void swap(optional< T > &lhs, optional< T > &rhs) noexcept(noexcept(lhs.swap(rhs)))
::std::size_t operator()(const tl::optional< T > &o) const
optional_copy_assign_base(const optional_copy_assign_base &rhs)=default
optional_copy_assign_base & operator=(const optional_copy_assign_base &rhs)
optional_copy_assign_base & operator=(optional_copy_assign_base &&rhs)=default
optional_copy_assign_base(optional_copy_assign_base &&rhs)=default
optional_copy_base & operator=(const optional_copy_base &rhs)=default
optional_copy_base(const optional_copy_base &rhs)
optional_copy_base & operator=(optional_copy_base &&rhs)=default
optional_copy_base(optional_copy_base &&rhs)=default
optional_delete_assign_base(const optional_delete_assign_base &)=default
optional_delete_assign_base(optional_delete_assign_base &&) noexcept=default
optional_delete_assign_base(optional_delete_assign_base &&) noexcept=default
optional_delete_assign_base(const optional_delete_assign_base &)=default
optional_delete_assign_base(const optional_delete_assign_base &)=default
optional_delete_assign_base(optional_delete_assign_base &&) noexcept=default
optional_delete_assign_base(optional_delete_assign_base &&) noexcept=default
optional_delete_assign_base(const optional_delete_assign_base &)=default
optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept=delete
optional_delete_ctor_base(const optional_delete_ctor_base &)=delete
optional_delete_ctor_base(const optional_delete_ctor_base &)=delete
optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept=default
optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept=delete
optional_delete_ctor_base(const optional_delete_ctor_base &)=default
optional_delete_ctor_base(const optional_delete_ctor_base &)=default
optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept=default
optional_move_assign_base & operator=(optional_move_assign_base &&rhs) noexcept(std::is_nothrow_move_constructible< T >::value &&std::is_nothrow_move_assignable< T >::value)
optional_move_assign_base(const optional_move_assign_base &rhs)=default
optional_move_assign_base & operator=(const optional_move_assign_base &rhs)=default
optional_move_assign_base(optional_move_assign_base &&rhs)=default
optional_move_base & operator=(const optional_move_base &rhs)=default
optional_move_base(const optional_move_base &rhs)=default
optional_move_base(optional_move_base &&rhs) noexcept(std::is_nothrow_move_constructible< T >::value)
optional_move_base & operator=(optional_move_base &&rhs)=default
constexpr const T && get() const &&
TL_OPTIONAL_11_CONSTEXPR T && get() &&
TL_OPTIONAL_11_CONSTEXPR T & get() &
TL_OPTIONAL_11_CONSTEXPR const T & get() const &
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u)
TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u)
TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept
A tag type to tell optional to construct its value in-place.
in_place_t()=default
A tag type to represent an empty optional.
constexpr nullopt_t(do_not_use, do_not_use) noexcept
#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T)
#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T)
#define TL_OPTIONAL_11_CONSTEXPR
#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T)