variant.hpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. // MPark.Variant
  2. //
  3. // Copyright Michael Park, 2015-2017
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. #ifndef MPARK_VARIANT_HPP
  8. #define MPARK_VARIANT_HPP
  9. /*
  10. variant synopsis
  11. namespace std {
  12. // 20.7.2, class template variant
  13. template <class... Types>
  14. class variant {
  15. public:
  16. // 20.7.2.1, constructors
  17. constexpr variant() noexcept(see below);
  18. variant(const variant&);
  19. variant(variant&&) noexcept(see below);
  20. template <class T> constexpr variant(T&&) noexcept(see below);
  21. template <class T, class... Args>
  22. constexpr explicit variant(in_place_type_t<T>, Args&&...);
  23. template <class T, class U, class... Args>
  24. constexpr explicit variant(
  25. in_place_type_t<T>, initializer_list<U>, Args&&...);
  26. template <size_t I, class... Args>
  27. constexpr explicit variant(in_place_index_t<I>, Args&&...);
  28. template <size_t I, class U, class... Args>
  29. constexpr explicit variant(
  30. in_place_index_t<I>, initializer_list<U>, Args&&...);
  31. // 20.7.2.2, destructor
  32. ~variant();
  33. // 20.7.2.3, assignment
  34. variant& operator=(const variant&);
  35. variant& operator=(variant&&) noexcept(see below);
  36. template <class T> variant& operator=(T&&) noexcept(see below);
  37. // 20.7.2.4, modifiers
  38. template <class T, class... Args>
  39. T& emplace(Args&&...);
  40. template <class T, class U, class... Args>
  41. T& emplace(initializer_list<U>, Args&&...);
  42. template <size_t I, class... Args>
  43. variant_alternative<I, variant>& emplace(Args&&...);
  44. template <size_t I, class U, class... Args>
  45. variant_alternative<I, variant>& emplace(initializer_list<U>, Args&&...);
  46. // 20.7.2.5, value status
  47. constexpr bool valueless_by_exception() const noexcept;
  48. constexpr size_t index() const noexcept;
  49. // 20.7.2.6, swap
  50. void swap(variant&) noexcept(see below);
  51. };
  52. // 20.7.3, variant helper classes
  53. template <class T> struct variant_size; // undefined
  54. template <class T>
  55. constexpr size_t variant_size_v = variant_size<T>::value;
  56. template <class T> struct variant_size<const T>;
  57. template <class T> struct variant_size<volatile T>;
  58. template <class T> struct variant_size<const volatile T>;
  59. template <class... Types>
  60. struct variant_size<variant<Types...>>;
  61. template <size_t I, class T> struct variant_alternative; // undefined
  62. template <size_t I, class T>
  63. using variant_alternative_t = typename variant_alternative<I, T>::type;
  64. template <size_t I, class T> struct variant_alternative<I, const T>;
  65. template <size_t I, class T> struct variant_alternative<I, volatile T>;
  66. template <size_t I, class T> struct variant_alternative<I, const volatile T>;
  67. template <size_t I, class... Types>
  68. struct variant_alternative<I, variant<Types...>>;
  69. constexpr size_t variant_npos = -1;
  70. // 20.7.4, value access
  71. template <class T, class... Types>
  72. constexpr bool holds_alternative(const variant<Types...>&) noexcept;
  73. template <size_t I, class... Types>
  74. constexpr variant_alternative_t<I, variant<Types...>>&
  75. get(variant<Types...>&);
  76. template <size_t I, class... Types>
  77. constexpr variant_alternative_t<I, variant<Types...>>&&
  78. get(variant<Types...>&&);
  79. template <size_t I, class... Types>
  80. constexpr variant_alternative_t<I, variant<Types...>> const&
  81. get(const variant<Types...>&);
  82. template <size_t I, class... Types>
  83. constexpr variant_alternative_t<I, variant<Types...>> const&&
  84. get(const variant<Types...>&&);
  85. template <class T, class... Types>
  86. constexpr T& get(variant<Types...>&);
  87. template <class T, class... Types>
  88. constexpr T&& get(variant<Types...>&&);
  89. template <class T, class... Types>
  90. constexpr const T& get(const variant<Types...>&);
  91. template <class T, class... Types>
  92. constexpr const T&& get(const variant<Types...>&&);
  93. template <size_t I, class... Types>
  94. constexpr add_pointer_t<variant_alternative_t<I, variant<Types...>>>
  95. get_if(variant<Types...>*) noexcept;
  96. template <size_t I, class... Types>
  97. constexpr add_pointer_t<const variant_alternative_t<I, variant<Types...>>>
  98. get_if(const variant<Types...>*) noexcept;
  99. template <class T, class... Types>
  100. constexpr add_pointer_t<T>
  101. get_if(variant<Types...>*) noexcept;
  102. template <class T, class... Types>
  103. constexpr add_pointer_t<const T>
  104. get_if(const variant<Types...>*) noexcept;
  105. // 20.7.5, relational operators
  106. template <class... Types>
  107. constexpr bool operator==(const variant<Types...>&, const variant<Types...>&);
  108. template <class... Types>
  109. constexpr bool operator!=(const variant<Types...>&, const variant<Types...>&);
  110. template <class... Types>
  111. constexpr bool operator<(const variant<Types...>&, const variant<Types...>&);
  112. template <class... Types>
  113. constexpr bool operator>(const variant<Types...>&, const variant<Types...>&);
  114. template <class... Types>
  115. constexpr bool operator<=(const variant<Types...>&, const variant<Types...>&);
  116. template <class... Types>
  117. constexpr bool operator>=(const variant<Types...>&, const variant<Types...>&);
  118. // 20.7.6, visitation
  119. template <class Visitor, class... Variants>
  120. constexpr see below visit(Visitor&&, Variants&&...);
  121. // 20.7.7, class monostate
  122. struct monostate;
  123. // 20.7.8, monostate relational operators
  124. constexpr bool operator<(monostate, monostate) noexcept;
  125. constexpr bool operator>(monostate, monostate) noexcept;
  126. constexpr bool operator<=(monostate, monostate) noexcept;
  127. constexpr bool operator>=(monostate, monostate) noexcept;
  128. constexpr bool operator==(monostate, monostate) noexcept;
  129. constexpr bool operator!=(monostate, monostate) noexcept;
  130. // 20.7.9, specialized algorithms
  131. template <class... Types>
  132. void swap(variant<Types...>&, variant<Types...>&) noexcept(see below);
  133. // 20.7.10, class bad_variant_access
  134. class bad_variant_access;
  135. // 20.7.11, hash support
  136. template <class T> struct hash;
  137. template <class... Types> struct hash<variant<Types...>>;
  138. template <> struct hash<monostate>;
  139. } // namespace std
  140. */
  141. #include <cstddef>
  142. #include <exception>
  143. #include <functional>
  144. #include <initializer_list>
  145. #include <new>
  146. #include <type_traits>
  147. #include <utility>
  148. // MPark.Variant
  149. //
  150. // Copyright Michael Park, 2015-2017
  151. //
  152. // Distributed under the Boost Software License, Version 1.0.
  153. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  154. #ifndef MPARK_CONFIG_HPP
  155. #define MPARK_CONFIG_HPP
  156. // MSVC 2015 Update 3.
  157. #if __cplusplus < 201103L && (!defined(_MSC_VER) || _MSC_FULL_VER < 190024215)
  158. #error "MPark.Variant requires C++11 support."
  159. #endif
  160. #ifndef __has_builtin
  161. #define __has_builtin(x) 0
  162. #endif
  163. #ifndef __has_feature
  164. #define __has_feature(x) 0
  165. #endif
  166. #if __has_builtin(__builtin_addressof) || \
  167. (defined(__GNUC__) && __GNUC__ >= 7) || defined(_MSC_VER)
  168. #define MPARK_BUILTIN_ADDRESSOF
  169. #endif
  170. #if __has_builtin(__type_pack_element)
  171. #define MPARK_TYPE_PACK_ELEMENT
  172. #endif
  173. #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
  174. #define MPARK_CPP14_CONSTEXPR
  175. #endif
  176. #if __has_feature(cxx_exceptions) || defined(__cpp_exceptions)
  177. #define MPARK_EXCEPTIONS
  178. #endif
  179. #if defined(__cpp_generic_lambdas) || defined(_MSC_VER)
  180. #define MPARK_GENERIC_LAMBDAS
  181. #endif
  182. #if defined(__cpp_lib_integer_sequence)
  183. #define MPARK_INTEGER_SEQUENCE
  184. #endif
  185. #if defined(__cpp_return_type_deduction) || defined(_MSC_VER)
  186. #define MPARK_RETURN_TYPE_DEDUCTION
  187. #endif
  188. #if defined(__cpp_lib_transparent_operators) || defined(_MSC_VER)
  189. #define MPARK_TRANSPARENT_OPERATORS
  190. #endif
  191. #if defined(__cpp_variable_templates) || defined(_MSC_VER)
  192. #define MPARK_VARIABLE_TEMPLATES
  193. #endif
  194. #if !defined(__GLIBCXX__)
  195. #define MPARK_TRIVIALITY_TYPE_TRAITS
  196. #elif defined(__has_include) && __has_include(<codecvt>) // >= libstdc++-5
  197. #define MPARK_TRIVIALITY_TYPE_TRAITS
  198. #endif
  199. #endif // MPARK_CONFIG_HPP
  200. // MPark.Variant
  201. //
  202. // Copyright Michael Park, 2015-2017
  203. //
  204. // Distributed under the Boost Software License, Version 1.0.
  205. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  206. #ifndef MPARK_IN_PLACE_HPP
  207. #define MPARK_IN_PLACE_HPP
  208. #include <cstddef>
  209. namespace mpark {
  210. struct in_place_t { explicit in_place_t() = default; };
  211. template <std::size_t I>
  212. struct in_place_index_t { explicit in_place_index_t() = default; };
  213. template <typename T>
  214. struct in_place_type_t { explicit in_place_type_t() = default; };
  215. #ifdef MPARK_VARIABLE_TEMPLATES
  216. constexpr in_place_t in_place{};
  217. template <std::size_t I> constexpr in_place_index_t<I> in_place_index{};
  218. template <typename T> constexpr in_place_type_t<T> in_place_type{};
  219. #endif
  220. } // namespace mpark
  221. #endif // MPARK_IN_PLACE_HPP
  222. // MPark.Variant
  223. //
  224. // Copyright Michael Park, 2015-2017
  225. //
  226. // Distributed under the Boost Software License, Version 1.0.
  227. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  228. #ifndef MPARK_LIB_HPP
  229. #define MPARK_LIB_HPP
  230. #include <memory>
  231. #include <functional>
  232. #include <type_traits>
  233. #include <utility>
  234. #define RETURN(...) \
  235. noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { \
  236. return __VA_ARGS__; \
  237. }
  238. namespace mpark {
  239. namespace lib {
  240. template <typename T>
  241. struct identity { using type = T; };
  242. inline namespace cpp14 {
  243. template <typename T, std::size_t N>
  244. struct array {
  245. constexpr const T &operator[](std::size_t index) const {
  246. return data[index];
  247. }
  248. T data[N == 0 ? 1 : N];
  249. };
  250. template <typename T>
  251. using add_pointer_t = typename std::add_pointer<T>::type;
  252. template <typename... Ts>
  253. using common_type_t = typename std::common_type<Ts...>::type;
  254. template <typename T>
  255. using decay_t = typename std::decay<T>::type;
  256. template <bool B, typename T = void>
  257. using enable_if_t = typename std::enable_if<B, T>::type;
  258. template <typename T>
  259. using remove_const_t = typename std::remove_const<T>::type;
  260. template <typename T>
  261. using remove_reference_t = typename std::remove_reference<T>::type;
  262. template <typename T>
  263. inline constexpr T &&forward(remove_reference_t<T> &t) noexcept {
  264. return static_cast<T &&>(t);
  265. }
  266. template <typename T>
  267. inline constexpr T &&forward(remove_reference_t<T> &&t) noexcept {
  268. static_assert(!std::is_lvalue_reference<T>::value,
  269. "can not forward an rvalue as an lvalue");
  270. return static_cast<T &&>(t);
  271. }
  272. template <typename T>
  273. inline constexpr remove_reference_t<T> &&move(T &&t) noexcept {
  274. return static_cast<remove_reference_t<T> &&>(t);
  275. }
  276. #ifdef MPARK_INTEGER_SEQUENCE
  277. template <typename T, T... Is>
  278. using integer_sequence = std::integer_sequence<T, Is...>;
  279. template <std::size_t... Is>
  280. using index_sequence = std::index_sequence<Is...>;
  281. template <std::size_t N>
  282. using make_index_sequence = std::make_index_sequence<N>;
  283. template <typename... Ts>
  284. using index_sequence_for = std::index_sequence_for<Ts...>;
  285. #else
  286. template <typename T, T... Is>
  287. struct integer_sequence {
  288. using value_type = T;
  289. static constexpr std::size_t size() noexcept { return sizeof...(Is); }
  290. };
  291. template <std::size_t... Is>
  292. using index_sequence = integer_sequence<std::size_t, Is...>;
  293. template <typename Lhs, typename Rhs>
  294. struct make_index_sequence_concat;
  295. template <std::size_t... Lhs, std::size_t... Rhs>
  296. struct make_index_sequence_concat<index_sequence<Lhs...>,
  297. index_sequence<Rhs...>>
  298. : identity<index_sequence<Lhs..., (sizeof...(Lhs) + Rhs)...>> {};
  299. template <std::size_t N>
  300. struct make_index_sequence_impl;
  301. template <std::size_t N>
  302. using make_index_sequence = typename make_index_sequence_impl<N>::type;
  303. template <std::size_t N>
  304. struct make_index_sequence_impl
  305. : make_index_sequence_concat<make_index_sequence<N / 2>,
  306. make_index_sequence<N - (N / 2)>> {};
  307. template <>
  308. struct make_index_sequence_impl<0> : identity<index_sequence<>> {};
  309. template <>
  310. struct make_index_sequence_impl<1> : identity<index_sequence<0>> {};
  311. template <typename... Ts>
  312. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  313. #endif
  314. // <functional>
  315. #ifdef MPARK_TRANSPARENT_OPERATORS
  316. using equal_to = std::equal_to<>;
  317. #else
  318. struct equal_to {
  319. template <typename Lhs, typename Rhs>
  320. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  321. RETURN(lib::forward<Lhs>(lhs) == lib::forward<Rhs>(rhs))
  322. };
  323. #endif
  324. #ifdef MPARK_TRANSPARENT_OPERATORS
  325. using not_equal_to = std::not_equal_to<>;
  326. #else
  327. struct not_equal_to {
  328. template <typename Lhs, typename Rhs>
  329. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  330. RETURN(lib::forward<Lhs>(lhs) != lib::forward<Rhs>(rhs))
  331. };
  332. #endif
  333. #ifdef MPARK_TRANSPARENT_OPERATORS
  334. using less = std::less<>;
  335. #else
  336. struct less {
  337. template <typename Lhs, typename Rhs>
  338. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  339. RETURN(lib::forward<Lhs>(lhs) < lib::forward<Rhs>(rhs))
  340. };
  341. #endif
  342. #ifdef MPARK_TRANSPARENT_OPERATORS
  343. using greater = std::greater<>;
  344. #else
  345. struct greater {
  346. template <typename Lhs, typename Rhs>
  347. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  348. RETURN(lib::forward<Lhs>(lhs) > lib::forward<Rhs>(rhs))
  349. };
  350. #endif
  351. #ifdef MPARK_TRANSPARENT_OPERATORS
  352. using less_equal = std::less_equal<>;
  353. #else
  354. struct less_equal {
  355. template <typename Lhs, typename Rhs>
  356. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  357. RETURN(lib::forward<Lhs>(lhs) <= lib::forward<Rhs>(rhs))
  358. };
  359. #endif
  360. #ifdef MPARK_TRANSPARENT_OPERATORS
  361. using greater_equal = std::greater_equal<>;
  362. #else
  363. struct greater_equal {
  364. template <typename Lhs, typename Rhs>
  365. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  366. RETURN(lib::forward<Lhs>(lhs) >= lib::forward<Rhs>(rhs))
  367. };
  368. #endif
  369. } // namespace cpp14
  370. inline namespace cpp17 {
  371. // <type_traits>
  372. template <bool B>
  373. using bool_constant = std::integral_constant<bool, B>;
  374. template <typename...>
  375. struct voider : identity<void> {};
  376. template <typename... Ts>
  377. using void_t = typename voider<Ts...>::type;
  378. namespace detail {
  379. namespace swappable {
  380. using std::swap;
  381. template <typename T>
  382. struct is_swappable_impl {
  383. private:
  384. template <typename U,
  385. typename = decltype(swap(std::declval<U &>(),
  386. std::declval<U &>()))>
  387. inline static std::true_type test(int);
  388. template <typename U>
  389. inline static std::false_type test(...);
  390. public:
  391. using type = decltype(test<T>(0));
  392. };
  393. template <typename T>
  394. using is_swappable = typename is_swappable_impl<T>::type;
  395. template <typename T, bool = is_swappable<T>::value>
  396. struct is_nothrow_swappable {
  397. static constexpr bool value =
  398. noexcept(swap(std::declval<T &>(), std::declval<T &>()));
  399. };
  400. template <typename T>
  401. struct is_nothrow_swappable<T, false> : std::false_type {};
  402. } // namespace swappable
  403. } // namespace detail
  404. template <typename T>
  405. using is_swappable = detail::swappable::is_swappable<T>;
  406. template <typename T>
  407. using is_nothrow_swappable = detail::swappable::is_nothrow_swappable<T>;
  408. // <functional>
  409. #ifdef _MSC_VER
  410. #pragma warning(push)
  411. #pragma warning(disable : 4100)
  412. #endif
  413. template <typename F, typename... As>
  414. inline constexpr auto invoke(F &&f, As &&... as)
  415. RETURN(lib::forward<F>(f)(lib::forward<As>(as)...))
  416. #ifdef _MSC_VER
  417. #pragma warning(pop)
  418. #endif
  419. template <typename B, typename T, typename D>
  420. inline constexpr auto invoke(T B::*pmv, D &&d)
  421. RETURN(lib::forward<D>(d).*pmv)
  422. template <typename Pmv, typename Ptr>
  423. inline constexpr auto invoke(Pmv pmv, Ptr &&ptr)
  424. RETURN((*lib::forward<Ptr>(ptr)).*pmv)
  425. template <typename B, typename T, typename D, typename... As>
  426. inline constexpr auto invoke(T B::*pmf, D &&d, As &&... as)
  427. RETURN((lib::forward<D>(d).*pmf)(lib::forward<As>(as)...))
  428. template <typename Pmf, typename Ptr, typename... As>
  429. inline constexpr auto invoke(Pmf pmf, Ptr &&ptr, As &&... as)
  430. RETURN(((*lib::forward<Ptr>(ptr)).*pmf)(lib::forward<As>(as)...))
  431. namespace detail {
  432. template <typename Void, typename, typename...>
  433. struct invoke_result {};
  434. template <typename F, typename... Args>
  435. struct invoke_result<void_t<decltype(lib::invoke(
  436. std::declval<F>(), std::declval<Args>()...))>,
  437. F,
  438. Args...>
  439. : identity<decltype(
  440. lib::invoke(std::declval<F>(), std::declval<Args>()...))> {};
  441. } // namespace detail
  442. template <typename F, typename... Args>
  443. using invoke_result = detail::invoke_result<void, F, Args...>;
  444. template <typename F, typename... Args>
  445. using invoke_result_t = typename invoke_result<F, Args...>::type;
  446. namespace detail {
  447. template <typename Void, typename, typename...>
  448. struct is_invocable : std::false_type {};
  449. template <typename F, typename... Args>
  450. struct is_invocable<void_t<invoke_result_t<F, Args...>>, F, Args...>
  451. : std::true_type {};
  452. template <typename Void, typename, typename, typename...>
  453. struct is_invocable_r : std::false_type {};
  454. template <typename R, typename F, typename... Args>
  455. struct is_invocable_r<void_t<invoke_result_t<F, Args...>>,
  456. R,
  457. F,
  458. Args...>
  459. : std::is_convertible<invoke_result_t<F, Args...>, R> {};
  460. } // namespace detail
  461. template <typename F, typename... Args>
  462. using is_invocable = detail::is_invocable<void, F, Args...>;
  463. template <typename R, typename F, typename... Args>
  464. using is_invocable_r = detail::is_invocable_r<void, R, F, Args...>;
  465. // <memory>
  466. #ifdef MPARK_BUILTIN_ADDRESSOF
  467. template <typename T>
  468. inline constexpr T *addressof(T &arg) {
  469. return __builtin_addressof(arg);
  470. }
  471. #else
  472. namespace detail {
  473. namespace has_addressof_impl {
  474. struct fail;
  475. template <typename T>
  476. inline fail operator&(T &&);
  477. template <typename T>
  478. inline static constexpr bool impl() {
  479. return (std::is_class<T>::value || std::is_union<T>::value) &&
  480. !std::is_same<decltype(&std::declval<T &>()), fail>::value;
  481. }
  482. } // namespace has_addressof_impl
  483. template <typename T>
  484. using has_addressof = bool_constant<has_addressof_impl::impl<T>()>;
  485. template <typename T>
  486. inline constexpr T *addressof(T &arg, std::true_type) {
  487. return std::addressof(arg);
  488. }
  489. template <typename T>
  490. inline constexpr T *addressof(T &arg, std::false_type) {
  491. return &arg;
  492. }
  493. } // namespace detail
  494. template <typename T>
  495. inline constexpr T *addressof(T &arg) {
  496. return detail::addressof(arg, detail::has_addressof<T>{});
  497. }
  498. #endif
  499. template <typename T>
  500. inline constexpr T *addressof(const T &&) = delete;
  501. } // namespace cpp17
  502. template <typename T>
  503. struct remove_all_extents : identity<T> {};
  504. template <typename T, std::size_t N>
  505. struct remove_all_extents<array<T, N>> : remove_all_extents<T> {};
  506. template <typename T>
  507. using remove_all_extents_t = typename remove_all_extents<T>::type;
  508. template <std::size_t N>
  509. using size_constant = std::integral_constant<std::size_t, N>;
  510. template <bool... Bs>
  511. using bool_sequence = integer_sequence<bool, Bs...>;
  512. template <std::size_t I, typename T>
  513. struct indexed_type : size_constant<I>, identity<T> {};
  514. template <bool... Bs>
  515. using all =
  516. std::is_same<bool_sequence<true, Bs...>, bool_sequence<Bs..., true>>;
  517. #ifdef MPARK_TYPE_PACK_ELEMENT
  518. template <std::size_t I, typename... Ts>
  519. using type_pack_element_t = __type_pack_element<I, Ts...>;
  520. #else
  521. template <std::size_t I, typename... Ts>
  522. struct type_pack_element_impl {
  523. private:
  524. template <typename>
  525. struct set;
  526. template <std::size_t... Is>
  527. struct set<index_sequence<Is...>> : indexed_type<Is, Ts>... {};
  528. template <typename T>
  529. inline static std::enable_if<true, T> impl(indexed_type<I, T>);
  530. inline static std::enable_if<false> impl(...);
  531. public:
  532. using type = decltype(impl(set<index_sequence_for<Ts...>>{}));
  533. };
  534. template <std::size_t I, typename... Ts>
  535. using type_pack_element = typename type_pack_element_impl<I, Ts...>::type;
  536. template <std::size_t I, typename... Ts>
  537. using type_pack_element_t = typename type_pack_element<I, Ts...>::type;
  538. #endif
  539. #ifdef MPARK_TRIVIALITY_TYPE_TRAITS
  540. template <typename T>
  541. using is_trivially_copy_constructible =
  542. std::is_trivially_copy_constructible<T>;
  543. template <typename T>
  544. using is_trivially_move_constructible =
  545. std::is_trivially_move_constructible<T>;
  546. template <typename T>
  547. using is_trivially_copy_assignable = std::is_trivially_copy_assignable<T>;
  548. template <typename T>
  549. using is_trivially_move_assignable = std::is_trivially_move_assignable<T>;
  550. #else
  551. template <typename T>
  552. struct is_trivially_copy_constructible
  553. : bool_constant<
  554. std::is_copy_constructible<T>::value && __has_trivial_copy(T)> {};
  555. template <typename T>
  556. struct is_trivially_move_constructible : bool_constant<__is_trivial(T)> {};
  557. template <typename T>
  558. struct is_trivially_copy_assignable
  559. : bool_constant<
  560. std::is_copy_assignable<T>::value && __has_trivial_assign(T)> {};
  561. template <typename T>
  562. struct is_trivially_move_assignable : bool_constant<__is_trivial(T)> {};
  563. #endif
  564. } // namespace lib
  565. } // namespace mpark
  566. #undef RETURN
  567. #endif // MPARK_LIB_HPP
  568. namespace mpark {
  569. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  570. #define AUTO auto
  571. #define AUTO_RETURN(...) { return __VA_ARGS__; }
  572. #define AUTO_REFREF auto &&
  573. #define AUTO_REFREF_RETURN(...) { return __VA_ARGS__; }
  574. #define DECLTYPE_AUTO decltype(auto)
  575. #define DECLTYPE_AUTO_RETURN(...) { return __VA_ARGS__; }
  576. #else
  577. #define AUTO auto
  578. #define AUTO_RETURN(...) \
  579. -> lib::decay_t<decltype(__VA_ARGS__)> { return __VA_ARGS__; }
  580. #define AUTO_REFREF auto
  581. #define AUTO_REFREF_RETURN(...) \
  582. -> decltype((__VA_ARGS__)) { \
  583. static_assert(std::is_reference<decltype((__VA_ARGS__))>::value, ""); \
  584. return __VA_ARGS__; \
  585. }
  586. #define DECLTYPE_AUTO auto
  587. #define DECLTYPE_AUTO_RETURN(...) \
  588. -> decltype(__VA_ARGS__) { return __VA_ARGS__; }
  589. #endif
  590. class bad_variant_access : public std::exception {
  591. public:
  592. virtual const char *what() const noexcept { return "bad_variant_access"; }
  593. };
  594. [[noreturn]] inline void throw_bad_variant_access() {
  595. #ifdef MPARK_EXCEPTIONS
  596. throw bad_variant_access{};
  597. #else
  598. std::terminate();
  599. #endif
  600. }
  601. template <typename... Ts>
  602. class variant;
  603. template <typename T>
  604. struct variant_size;
  605. #ifdef MPARK_VARIABLE_TEMPLATES
  606. template <typename T>
  607. constexpr std::size_t variant_size_v = variant_size<T>::value;
  608. #endif
  609. template <typename T>
  610. struct variant_size<const T> : variant_size<T> {};
  611. template <typename T>
  612. struct variant_size<volatile T> : variant_size<T> {};
  613. template <typename T>
  614. struct variant_size<const volatile T> : variant_size<T> {};
  615. template <typename... Ts>
  616. struct variant_size<variant<Ts...>> : lib::size_constant<sizeof...(Ts)> {};
  617. template <std::size_t I, typename T>
  618. struct variant_alternative;
  619. template <std::size_t I, typename T>
  620. using variant_alternative_t = typename variant_alternative<I, T>::type;
  621. template <std::size_t I, typename T>
  622. struct variant_alternative<I, const T>
  623. : std::add_const<variant_alternative_t<I, T>> {};
  624. template <std::size_t I, typename T>
  625. struct variant_alternative<I, volatile T>
  626. : std::add_volatile<variant_alternative_t<I, T>> {};
  627. template <std::size_t I, typename T>
  628. struct variant_alternative<I, const volatile T>
  629. : std::add_cv<variant_alternative_t<I, T>> {};
  630. template <std::size_t I, typename... Ts>
  631. struct variant_alternative<I, variant<Ts...>>
  632. : lib::identity<lib::type_pack_element_t<I, Ts...>> {
  633. static_assert(I < sizeof...(Ts),
  634. "`variant_alternative` index out of range.");
  635. };
  636. constexpr std::size_t variant_npos = static_cast<std::size_t>(-1);
  637. namespace detail {
  638. inline constexpr bool all() { return true; }
  639. template <typename... Bs>
  640. inline constexpr bool all(bool b, Bs... bs) {
  641. return b && all(bs...);
  642. }
  643. constexpr std::size_t not_found = static_cast<std::size_t>(-1);
  644. constexpr std::size_t ambiguous = static_cast<std::size_t>(-2);
  645. #ifdef MPARK_CPP14_CONSTEXPR
  646. template <typename T, typename... Ts>
  647. inline constexpr std::size_t find_index() {
  648. constexpr lib::array<bool, sizeof...(Ts)> matches = {
  649. {std::is_same<T, Ts>::value...}
  650. };
  651. std::size_t result = not_found;
  652. for (std::size_t i = 0; i < sizeof...(Ts); ++i) {
  653. if (matches[i]) {
  654. if (result != not_found) {
  655. return ambiguous;
  656. }
  657. result = i;
  658. }
  659. }
  660. return result;
  661. }
  662. #else
  663. inline constexpr std::size_t find_index_impl(std::size_t result,
  664. std::size_t) {
  665. return result;
  666. }
  667. template <typename... Bs>
  668. inline constexpr std::size_t find_index_impl(std::size_t result,
  669. std::size_t idx,
  670. bool b,
  671. Bs... bs) {
  672. return b ? (result != not_found ? ambiguous
  673. : find_index_impl(idx, idx + 1, bs...))
  674. : find_index_impl(result, idx + 1, bs...);
  675. }
  676. template <typename T, typename... Ts>
  677. inline constexpr std::size_t find_index() {
  678. return find_index_impl(not_found, 0, std::is_same<T, Ts>::value...);
  679. }
  680. #endif
  681. template <std::size_t I>
  682. using find_index_sfinae_impl =
  683. lib::enable_if_t<I != not_found && I != ambiguous,
  684. lib::size_constant<I>>;
  685. template <typename T, typename... Ts>
  686. using find_index_sfinae = find_index_sfinae_impl<find_index<T, Ts...>()>;
  687. template <std::size_t I>
  688. struct find_index_checked_impl : lib::size_constant<I> {
  689. static_assert(I != not_found, "the specified type is not found.");
  690. static_assert(I != ambiguous, "the specified type is ambiguous.");
  691. };
  692. template <typename T, typename... Ts>
  693. using find_index_checked = find_index_checked_impl<find_index<T, Ts...>()>;
  694. struct valueless_t {};
  695. enum class Trait { TriviallyAvailable, Available, Unavailable };
  696. template <typename T,
  697. template <typename> class IsTriviallyAvailable,
  698. template <typename> class IsAvailable>
  699. inline constexpr Trait trait() {
  700. return IsTriviallyAvailable<T>::value
  701. ? Trait::TriviallyAvailable
  702. : IsAvailable<T>::value ? Trait::Available
  703. : Trait::Unavailable;
  704. }
  705. #ifdef MPARK_CPP14_CONSTEXPR
  706. template <typename... Traits>
  707. inline constexpr Trait common_trait(Traits... traits) {
  708. Trait result = Trait::TriviallyAvailable;
  709. for (Trait t : {traits...}) {
  710. if (static_cast<int>(t) > static_cast<int>(result)) {
  711. result = t;
  712. }
  713. }
  714. return result;
  715. }
  716. #else
  717. inline constexpr Trait common_trait_impl(Trait result) { return result; }
  718. template <typename... Traits>
  719. inline constexpr Trait common_trait_impl(Trait result,
  720. Trait t,
  721. Traits... ts) {
  722. return static_cast<int>(t) > static_cast<int>(result)
  723. ? common_trait_impl(t, ts...)
  724. : common_trait_impl(result, ts...);
  725. }
  726. template <typename... Traits>
  727. inline constexpr Trait common_trait(Traits... ts) {
  728. return common_trait_impl(Trait::TriviallyAvailable, ts...);
  729. }
  730. #endif
  731. template <typename... Ts>
  732. struct traits {
  733. static constexpr Trait copy_constructible_trait =
  734. common_trait(trait<Ts,
  735. lib::is_trivially_copy_constructible,
  736. std::is_copy_constructible>()...);
  737. static constexpr Trait move_constructible_trait =
  738. common_trait(trait<Ts,
  739. lib::is_trivially_move_constructible,
  740. std::is_move_constructible>()...);
  741. static constexpr Trait copy_assignable_trait =
  742. common_trait(copy_constructible_trait,
  743. trait<Ts,
  744. lib::is_trivially_copy_assignable,
  745. std::is_copy_assignable>()...);
  746. static constexpr Trait move_assignable_trait =
  747. common_trait(move_constructible_trait,
  748. trait<Ts,
  749. lib::is_trivially_move_assignable,
  750. std::is_move_assignable>()...);
  751. static constexpr Trait destructible_trait =
  752. common_trait(trait<Ts,
  753. std::is_trivially_destructible,
  754. std::is_destructible>()...);
  755. };
  756. namespace access {
  757. struct recursive_union {
  758. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  759. template <typename V>
  760. inline static constexpr auto &&get_alt(V &&v, in_place_index_t<0>) {
  761. return lib::forward<V>(v).head_;
  762. }
  763. template <typename V, std::size_t I>
  764. inline static constexpr auto &&get_alt(V &&v, in_place_index_t<I>) {
  765. return get_alt(lib::forward<V>(v).tail_, in_place_index_t<I - 1>{});
  766. }
  767. #else
  768. template <std::size_t I, bool Dummy = true>
  769. struct get_alt_impl {
  770. template <typename V>
  771. inline constexpr AUTO_REFREF operator()(V &&v) const
  772. AUTO_REFREF_RETURN(get_alt_impl<I - 1>{}(lib::forward<V>(v).tail_))
  773. };
  774. template <bool Dummy>
  775. struct get_alt_impl<0, Dummy> {
  776. template <typename V>
  777. inline constexpr AUTO_REFREF operator()(V &&v) const
  778. AUTO_REFREF_RETURN(lib::forward<V>(v).head_)
  779. };
  780. template <typename V, std::size_t I>
  781. inline static constexpr AUTO_REFREF get_alt(V &&v, in_place_index_t<I>)
  782. AUTO_REFREF_RETURN(get_alt_impl<I>{}(lib::forward<V>(v)))
  783. #endif
  784. };
  785. struct base {
  786. template <std::size_t I, typename V>
  787. inline static constexpr AUTO_REFREF get_alt(V &&v)
  788. AUTO_REFREF_RETURN(recursive_union::get_alt(
  789. data(lib::forward<V>(v)), in_place_index_t<I>{}))
  790. };
  791. struct variant {
  792. template <std::size_t I, typename V>
  793. inline static constexpr AUTO_REFREF get_alt(V &&v)
  794. AUTO_REFREF_RETURN(base::get_alt<I>(lib::forward<V>(v).impl_))
  795. };
  796. } // namespace access
  797. namespace visitation {
  798. struct base {
  799. private:
  800. template <typename T>
  801. inline static constexpr const T &at(const T &elem) {
  802. return elem;
  803. }
  804. template <typename T, std::size_t N, typename... Is>
  805. inline static constexpr const lib::remove_all_extents_t<T> &at(
  806. const lib::array<T, N> &elems, std::size_t i, Is... is) {
  807. return at(elems[i], is...);
  808. }
  809. template <typename F, typename... Fs>
  810. inline static constexpr int visit_visitor_return_type_check() {
  811. static_assert(all(std::is_same<F, Fs>::value...),
  812. "`mpark::visit` requires the visitor to have a single "
  813. "return type.");
  814. return 0;
  815. }
  816. template <typename... Fs>
  817. inline static constexpr lib::array<
  818. lib::common_type_t<lib::decay_t<Fs>...>,
  819. sizeof...(Fs)>
  820. make_farray(Fs &&... fs) {
  821. using result = lib::array<lib::common_type_t<lib::decay_t<Fs>...>,
  822. sizeof...(Fs)>;
  823. return visit_visitor_return_type_check<lib::decay_t<Fs>...>(),
  824. result{{lib::forward<Fs>(fs)...}};
  825. }
  826. template <std::size_t... Is>
  827. struct dispatcher {
  828. template <typename F, typename... Vs>
  829. struct impl {
  830. inline static constexpr DECLTYPE_AUTO dispatch(F f, Vs... vs)
  831. DECLTYPE_AUTO_RETURN(lib::invoke(
  832. static_cast<F>(f),
  833. access::base::get_alt<Is>(static_cast<Vs>(vs))...))
  834. };
  835. };
  836. template <typename F, typename... Vs, std::size_t... Is>
  837. inline static constexpr AUTO make_dispatch(lib::index_sequence<Is...>)
  838. AUTO_RETURN(&dispatcher<Is...>::template impl<F, Vs...>::dispatch)
  839. template <std::size_t I, typename F, typename... Vs>
  840. inline static constexpr AUTO make_fdiagonal_impl()
  841. AUTO_RETURN(make_dispatch<F, Vs...>(
  842. lib::index_sequence<lib::indexed_type<I, Vs>::value...>{}))
  843. template <typename F, typename... Vs, std::size_t... Is>
  844. inline static constexpr AUTO make_fdiagonal_impl(
  845. lib::index_sequence<Is...>)
  846. AUTO_RETURN(make_farray(make_fdiagonal_impl<Is, F, Vs...>()...))
  847. template <typename F, typename V, typename... Vs>
  848. inline static constexpr /* auto * */ auto make_fdiagonal()
  849. -> decltype(make_fdiagonal_impl<F, V, Vs...>(
  850. lib::make_index_sequence<lib::decay_t<V>::size()>{})) {
  851. static_assert(
  852. all((lib::decay_t<V>::size() == lib::decay_t<Vs>::size())...),
  853. "all of the variants must be the same size.");
  854. return make_fdiagonal_impl<F, V, Vs...>(
  855. lib::make_index_sequence<lib::decay_t<V>::size()>{});
  856. }
  857. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  858. template <typename F, typename... Vs, std::size_t... Is>
  859. inline static constexpr auto make_fmatrix_impl(
  860. lib::index_sequence<Is...> is) {
  861. return make_dispatch<F, Vs...>(is);
  862. }
  863. template <typename F,
  864. typename... Vs,
  865. std::size_t... Is,
  866. std::size_t... Js,
  867. typename... Ls>
  868. inline static constexpr auto make_fmatrix_impl(
  869. lib::index_sequence<Is...>, lib::index_sequence<Js...>, Ls... ls) {
  870. return make_farray(make_fmatrix_impl<F, Vs...>(
  871. lib::index_sequence<Is..., Js>{}, ls...)...);
  872. }
  873. template <typename F, typename... Vs>
  874. inline static constexpr auto make_fmatrix() {
  875. return make_fmatrix_impl<F, Vs...>(
  876. lib::index_sequence<>{},
  877. lib::make_index_sequence<lib::decay_t<Vs>::size()>{}...);
  878. }
  879. #else
  880. template <typename F, typename... Vs>
  881. struct make_fmatrix_impl {
  882. template <typename...>
  883. struct impl;
  884. template <std::size_t... Is>
  885. struct impl<lib::index_sequence<Is...>> {
  886. inline constexpr AUTO operator()() const
  887. AUTO_RETURN(
  888. make_dispatch<F, Vs...>(lib::index_sequence<Is...>{}))
  889. };
  890. template <std::size_t... Is, std::size_t... Js, typename... Ls>
  891. struct impl<lib::index_sequence<Is...>,
  892. lib::index_sequence<Js...>,
  893. Ls...> {
  894. inline constexpr AUTO operator()() const
  895. AUTO_RETURN(make_farray(
  896. impl<lib::index_sequence<Is..., Js>, Ls...>{}()...))
  897. };
  898. };
  899. template <typename F, typename... Vs>
  900. inline static constexpr AUTO make_fmatrix()
  901. AUTO_RETURN(
  902. typename make_fmatrix_impl<F, Vs...>::template impl<
  903. lib::index_sequence<>,
  904. lib::make_index_sequence<lib::decay_t<Vs>::size()>...>{}())
  905. #endif
  906. public:
  907. template <typename Visitor, typename... Vs>
  908. inline static constexpr DECLTYPE_AUTO visit_alt_at(std::size_t index,
  909. Visitor &&visitor,
  910. Vs &&... vs)
  911. DECLTYPE_AUTO_RETURN(
  912. at(make_fdiagonal<Visitor &&,
  913. decltype(as_base(lib::forward<Vs>(vs)))...>(),
  914. index)(lib::forward<Visitor>(visitor),
  915. as_base(lib::forward<Vs>(vs))...))
  916. template <typename Visitor, typename... Vs>
  917. inline static constexpr DECLTYPE_AUTO visit_alt(Visitor &&visitor,
  918. Vs &&... vs)
  919. DECLTYPE_AUTO_RETURN(
  920. at(make_fmatrix<Visitor &&,
  921. decltype(as_base(lib::forward<Vs>(vs)))...>(),
  922. vs.index()...)(lib::forward<Visitor>(visitor),
  923. as_base(lib::forward<Vs>(vs))...))
  924. };
  925. struct variant {
  926. private:
  927. template <typename Visitor, typename... Values>
  928. struct visit_exhaustive_visitor_check {
  929. static_assert(
  930. lib::is_invocable<Visitor, Values...>::value,
  931. "`mpark::visit` requires the visitor to be exhaustive.");
  932. #ifdef _MSC_VER
  933. #pragma warning(push)
  934. #pragma warning(disable : 4100)
  935. #endif
  936. inline constexpr DECLTYPE_AUTO operator()(Visitor &&visitor,
  937. Values &&... values) const
  938. DECLTYPE_AUTO_RETURN(lib::invoke(lib::forward<Visitor>(visitor),
  939. lib::forward<Values>(values)...))
  940. #ifdef _MSC_VER
  941. #pragma warning(pop)
  942. #endif
  943. };
  944. template <typename Visitor>
  945. struct value_visitor {
  946. Visitor &&visitor_;
  947. template <typename... Alts>
  948. inline constexpr DECLTYPE_AUTO operator()(Alts &&... alts) const
  949. DECLTYPE_AUTO_RETURN(
  950. visit_exhaustive_visitor_check<
  951. Visitor,
  952. decltype((lib::forward<Alts>(alts).value))...>{}(
  953. lib::forward<Visitor>(visitor_),
  954. lib::forward<Alts>(alts).value...))
  955. };
  956. template <typename Visitor>
  957. inline static constexpr AUTO make_value_visitor(Visitor &&visitor)
  958. AUTO_RETURN(value_visitor<Visitor>{lib::forward<Visitor>(visitor)})
  959. public:
  960. template <typename Visitor, typename... Vs>
  961. inline static constexpr DECLTYPE_AUTO visit_alt_at(std::size_t index,
  962. Visitor &&visitor,
  963. Vs &&... vs)
  964. DECLTYPE_AUTO_RETURN(
  965. base::visit_alt_at(index,
  966. lib::forward<Visitor>(visitor),
  967. lib::forward<Vs>(vs).impl_...))
  968. template <typename Visitor, typename... Vs>
  969. inline static constexpr DECLTYPE_AUTO visit_alt(Visitor &&visitor,
  970. Vs &&... vs)
  971. DECLTYPE_AUTO_RETURN(base::visit_alt(lib::forward<Visitor>(visitor),
  972. lib::forward<Vs>(vs).impl_...))
  973. template <typename Visitor, typename... Vs>
  974. inline static constexpr DECLTYPE_AUTO visit_value_at(std::size_t index,
  975. Visitor &&visitor,
  976. Vs &&... vs)
  977. DECLTYPE_AUTO_RETURN(
  978. visit_alt_at(index,
  979. make_value_visitor(lib::forward<Visitor>(visitor)),
  980. lib::forward<Vs>(vs)...))
  981. template <typename Visitor, typename... Vs>
  982. inline static constexpr DECLTYPE_AUTO visit_value(Visitor &&visitor,
  983. Vs &&... vs)
  984. DECLTYPE_AUTO_RETURN(
  985. visit_alt(make_value_visitor(lib::forward<Visitor>(visitor)),
  986. lib::forward<Vs>(vs)...))
  987. };
  988. } // namespace visitation
  989. template <std::size_t Index, typename T>
  990. struct alt {
  991. using value_type = T;
  992. #ifdef _MSC_VER
  993. #pragma warning(push)
  994. #pragma warning(disable : 4244)
  995. #endif
  996. template <typename... Args>
  997. inline explicit constexpr alt(in_place_t, Args &&... args)
  998. : value(lib::forward<Args>(args)...) {}
  999. #ifdef _MSC_VER
  1000. #pragma warning(pop)
  1001. #endif
  1002. T value;
  1003. };
  1004. template <Trait DestructibleTrait, std::size_t Index, typename... Ts>
  1005. union recursive_union;
  1006. template <Trait DestructibleTrait, std::size_t Index>
  1007. union recursive_union<DestructibleTrait, Index> {};
  1008. #define MPARK_VARIANT_RECURSIVE_UNION(destructible_trait, destructor) \
  1009. template <std::size_t Index, typename T, typename... Ts> \
  1010. union recursive_union<destructible_trait, Index, T, Ts...> { \
  1011. public: \
  1012. inline explicit constexpr recursive_union(valueless_t) noexcept \
  1013. : dummy_{} {} \
  1014. \
  1015. template <typename... Args> \
  1016. inline explicit constexpr recursive_union(in_place_index_t<0>, \
  1017. Args &&... args) \
  1018. : head_(in_place_t{}, lib::forward<Args>(args)...) {} \
  1019. \
  1020. template <std::size_t I, typename... Args> \
  1021. inline explicit constexpr recursive_union(in_place_index_t<I>, \
  1022. Args &&... args) \
  1023. : tail_(in_place_index_t<I - 1>{}, lib::forward<Args>(args)...) {} \
  1024. \
  1025. recursive_union(const recursive_union &) = default; \
  1026. recursive_union(recursive_union &&) = default; \
  1027. \
  1028. destructor \
  1029. \
  1030. recursive_union &operator=(const recursive_union &) = default; \
  1031. recursive_union &operator=(recursive_union &&) = default; \
  1032. \
  1033. private: \
  1034. char dummy_; \
  1035. alt<Index, T> head_; \
  1036. recursive_union<destructible_trait, Index + 1, Ts...> tail_; \
  1037. \
  1038. friend struct access::recursive_union; \
  1039. }
  1040. MPARK_VARIANT_RECURSIVE_UNION(Trait::TriviallyAvailable,
  1041. ~recursive_union() = default;);
  1042. MPARK_VARIANT_RECURSIVE_UNION(Trait::Available,
  1043. ~recursive_union() {});
  1044. MPARK_VARIANT_RECURSIVE_UNION(Trait::Unavailable,
  1045. ~recursive_union() = delete;);
  1046. #undef MPARK_VARIANT_RECURSIVE_UNION
  1047. using index_t = unsigned int;
  1048. template <Trait DestructibleTrait, typename... Ts>
  1049. class base {
  1050. public:
  1051. inline explicit constexpr base(valueless_t tag) noexcept
  1052. : data_(tag), index_(static_cast<index_t>(-1)) {}
  1053. template <std::size_t I, typename... Args>
  1054. inline explicit constexpr base(in_place_index_t<I>, Args &&... args)
  1055. : data_(in_place_index_t<I>{}, lib::forward<Args>(args)...),
  1056. index_(I) {}
  1057. inline constexpr bool valueless_by_exception() const noexcept {
  1058. return index_ == static_cast<index_t>(-1);
  1059. }
  1060. inline constexpr std::size_t index() const noexcept {
  1061. return valueless_by_exception() ? variant_npos : index_;
  1062. }
  1063. protected:
  1064. using data_t = recursive_union<DestructibleTrait, 0, Ts...>;
  1065. friend inline constexpr base &as_base(base &b) { return b; }
  1066. friend inline constexpr const base &as_base(const base &b) { return b; }
  1067. friend inline constexpr base &&as_base(base &&b) { return lib::move(b); }
  1068. friend inline constexpr const base &&as_base(const base &&b) { return lib::move(b); }
  1069. friend inline constexpr data_t &data(base &b) { return b.data_; }
  1070. friend inline constexpr const data_t &data(const base &b) { return b.data_; }
  1071. friend inline constexpr data_t &&data(base &&b) { return lib::move(b).data_; }
  1072. friend inline constexpr const data_t &&data(const base &&b) { return lib::move(b).data_; }
  1073. inline static constexpr std::size_t size() { return sizeof...(Ts); }
  1074. data_t data_;
  1075. index_t index_;
  1076. friend struct access::base;
  1077. friend struct visitation::base;
  1078. };
  1079. struct dtor {
  1080. #ifdef _MSC_VER
  1081. #pragma warning(push)
  1082. #pragma warning(disable : 4100)
  1083. #endif
  1084. template <typename Alt>
  1085. inline void operator()(Alt &alt) const noexcept { alt.~Alt(); }
  1086. #ifdef _MSC_VER
  1087. #pragma warning(pop)
  1088. #endif
  1089. };
  1090. #if defined(_MSC_VER) && _MSC_VER < 1910
  1091. #define INHERITING_CTOR(type, base) \
  1092. template <typename... Args> \
  1093. inline explicit constexpr type(Args &&... args) \
  1094. : base(lib::forward<Args>(args)...) {}
  1095. #else
  1096. #define INHERITING_CTOR(type, base) using base::base;
  1097. #endif
  1098. template <typename Traits, Trait = Traits::destructible_trait>
  1099. class destructor;
  1100. #define MPARK_VARIANT_DESTRUCTOR(destructible_trait, definition, destroy) \
  1101. template <typename... Ts> \
  1102. class destructor<traits<Ts...>, destructible_trait> \
  1103. : public base<destructible_trait, Ts...> { \
  1104. using super = base<destructible_trait, Ts...>; \
  1105. \
  1106. public: \
  1107. INHERITING_CTOR(destructor, super) \
  1108. using super::operator=; \
  1109. \
  1110. destructor(const destructor &) = default; \
  1111. destructor(destructor &&) = default; \
  1112. definition \
  1113. destructor &operator=(const destructor &) = default; \
  1114. destructor &operator=(destructor &&) = default; \
  1115. \
  1116. protected: \
  1117. destroy \
  1118. }
  1119. MPARK_VARIANT_DESTRUCTOR(
  1120. Trait::TriviallyAvailable,
  1121. ~destructor() = default;,
  1122. inline void destroy() noexcept {
  1123. this->index_ = static_cast<index_t>(-1);
  1124. });
  1125. MPARK_VARIANT_DESTRUCTOR(
  1126. Trait::Available,
  1127. ~destructor() { destroy(); },
  1128. inline void destroy() noexcept {
  1129. if (!this->valueless_by_exception()) {
  1130. visitation::base::visit_alt(dtor{}, *this);
  1131. }
  1132. this->index_ = static_cast<index_t>(-1);
  1133. });
  1134. MPARK_VARIANT_DESTRUCTOR(
  1135. Trait::Unavailable,
  1136. ~destructor() = delete;,
  1137. inline void destroy() noexcept = delete;);
  1138. #undef MPARK_VARIANT_DESTRUCTOR
  1139. template <typename Traits>
  1140. class constructor : public destructor<Traits> {
  1141. using super = destructor<Traits>;
  1142. public:
  1143. INHERITING_CTOR(constructor, super)
  1144. using super::operator=;
  1145. protected:
  1146. #ifndef MPARK_GENERIC_LAMBDAS
  1147. struct ctor {
  1148. template <typename LhsAlt, typename RhsAlt>
  1149. inline void operator()(LhsAlt &lhs_alt, RhsAlt &&rhs_alt) const {
  1150. constructor::construct_alt(lhs_alt,
  1151. lib::forward<RhsAlt>(rhs_alt).value);
  1152. }
  1153. };
  1154. #endif
  1155. template <std::size_t I, typename T, typename... Args>
  1156. inline static T &construct_alt(alt<I, T> &a, Args &&... args) {
  1157. ::new (static_cast<void *>(lib::addressof(a)))
  1158. alt<I, T>(in_place_t{}, lib::forward<Args>(args)...);
  1159. return a.value;
  1160. }
  1161. template <typename Rhs>
  1162. inline static void generic_construct(constructor &lhs, Rhs &&rhs) {
  1163. lhs.destroy();
  1164. if (!rhs.valueless_by_exception()) {
  1165. visitation::base::visit_alt_at(
  1166. rhs.index(),
  1167. #ifdef MPARK_GENERIC_LAMBDAS
  1168. [](auto &lhs_alt, auto &&rhs_alt) {
  1169. constructor::construct_alt(
  1170. lhs_alt, lib::forward<decltype(rhs_alt)>(rhs_alt).value);
  1171. }
  1172. #else
  1173. ctor{}
  1174. #endif
  1175. ,
  1176. lhs,
  1177. lib::forward<Rhs>(rhs));
  1178. lhs.index_ = rhs.index_;
  1179. }
  1180. }
  1181. };
  1182. template <typename Traits, Trait = Traits::move_constructible_trait>
  1183. class move_constructor;
  1184. #define MPARK_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, definition) \
  1185. template <typename... Ts> \
  1186. class move_constructor<traits<Ts...>, move_constructible_trait> \
  1187. : public constructor<traits<Ts...>> { \
  1188. using super = constructor<traits<Ts...>>; \
  1189. \
  1190. public: \
  1191. INHERITING_CTOR(move_constructor, super) \
  1192. using super::operator=; \
  1193. \
  1194. move_constructor(const move_constructor &) = default; \
  1195. definition \
  1196. ~move_constructor() = default; \
  1197. move_constructor &operator=(const move_constructor &) = default; \
  1198. move_constructor &operator=(move_constructor &&) = default; \
  1199. }
  1200. MPARK_VARIANT_MOVE_CONSTRUCTOR(
  1201. Trait::TriviallyAvailable,
  1202. move_constructor(move_constructor &&that) = default;);
  1203. MPARK_VARIANT_MOVE_CONSTRUCTOR(
  1204. Trait::Available,
  1205. move_constructor(move_constructor &&that) noexcept(
  1206. all(std::is_nothrow_move_constructible<Ts>::value...))
  1207. : move_constructor(valueless_t{}) {
  1208. this->generic_construct(*this, lib::move(that));
  1209. });
  1210. MPARK_VARIANT_MOVE_CONSTRUCTOR(
  1211. Trait::Unavailable,
  1212. move_constructor(move_constructor &&) = delete;);
  1213. #undef MPARK_VARIANT_MOVE_CONSTRUCTOR
  1214. template <typename Traits, Trait = Traits::copy_constructible_trait>
  1215. class copy_constructor;
  1216. #define MPARK_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, definition) \
  1217. template <typename... Ts> \
  1218. class copy_constructor<traits<Ts...>, copy_constructible_trait> \
  1219. : public move_constructor<traits<Ts...>> { \
  1220. using super = move_constructor<traits<Ts...>>; \
  1221. \
  1222. public: \
  1223. INHERITING_CTOR(copy_constructor, super) \
  1224. using super::operator=; \
  1225. \
  1226. definition \
  1227. copy_constructor(copy_constructor &&) = default; \
  1228. ~copy_constructor() = default; \
  1229. copy_constructor &operator=(const copy_constructor &) = default; \
  1230. copy_constructor &operator=(copy_constructor &&) = default; \
  1231. }
  1232. MPARK_VARIANT_COPY_CONSTRUCTOR(
  1233. Trait::TriviallyAvailable,
  1234. copy_constructor(const copy_constructor &that) = default;);
  1235. MPARK_VARIANT_COPY_CONSTRUCTOR(
  1236. Trait::Available,
  1237. copy_constructor(const copy_constructor &that)
  1238. : copy_constructor(valueless_t{}) {
  1239. this->generic_construct(*this, that);
  1240. });
  1241. MPARK_VARIANT_COPY_CONSTRUCTOR(
  1242. Trait::Unavailable,
  1243. copy_constructor(const copy_constructor &) = delete;);
  1244. #undef MPARK_VARIANT_COPY_CONSTRUCTOR
  1245. template <typename Traits>
  1246. class assignment : public copy_constructor<Traits> {
  1247. using super = copy_constructor<Traits>;
  1248. public:
  1249. INHERITING_CTOR(assignment, super)
  1250. using super::operator=;
  1251. template <std::size_t I, typename... Args>
  1252. inline /* auto & */ auto emplace(Args &&... args)
  1253. -> decltype(this->construct_alt(access::base::get_alt<I>(*this),
  1254. lib::forward<Args>(args)...)) {
  1255. this->destroy();
  1256. auto &result = this->construct_alt(access::base::get_alt<I>(*this),
  1257. lib::forward<Args>(args)...);
  1258. this->index_ = I;
  1259. return result;
  1260. }
  1261. protected:
  1262. #ifndef MPARK_GENERIC_LAMBDAS
  1263. template <typename That>
  1264. struct assigner {
  1265. template <typename ThisAlt, typename ThatAlt>
  1266. inline void operator()(ThisAlt &this_alt, ThatAlt &&that_alt) const {
  1267. self->assign_alt(this_alt, lib::forward<ThatAlt>(that_alt).value);
  1268. }
  1269. assignment *self;
  1270. };
  1271. #endif
  1272. template <std::size_t I, typename T, typename Arg>
  1273. inline void assign_alt(alt<I, T> &a, Arg &&arg) {
  1274. if (this->index() == I) {
  1275. #ifdef _MSC_VER
  1276. #pragma warning(push)
  1277. #pragma warning(disable : 4244)
  1278. #endif
  1279. a.value = lib::forward<Arg>(arg);
  1280. #ifdef _MSC_VER
  1281. #pragma warning(pop)
  1282. #endif
  1283. } else {
  1284. struct {
  1285. void operator()(std::true_type) const {
  1286. this_->emplace<I>(lib::forward<Arg>(arg_));
  1287. }
  1288. void operator()(std::false_type) const {
  1289. this_->emplace<I>(T(lib::forward<Arg>(arg_)));
  1290. }
  1291. assignment *this_;
  1292. Arg &&arg_;
  1293. } impl{this, lib::forward<Arg>(arg)};
  1294. impl(lib::bool_constant<
  1295. std::is_nothrow_constructible<T, Arg>::value ||
  1296. !std::is_nothrow_move_constructible<T>::value>{});
  1297. }
  1298. }
  1299. template <typename That>
  1300. inline void generic_assign(That &&that) {
  1301. if (this->valueless_by_exception() && that.valueless_by_exception()) {
  1302. // do nothing.
  1303. } else if (that.valueless_by_exception()) {
  1304. this->destroy();
  1305. } else {
  1306. visitation::base::visit_alt_at(
  1307. that.index(),
  1308. #ifdef MPARK_GENERIC_LAMBDAS
  1309. [this](auto &this_alt, auto &&that_alt) {
  1310. this->assign_alt(
  1311. this_alt, lib::forward<decltype(that_alt)>(that_alt).value);
  1312. }
  1313. #else
  1314. assigner<That>{this}
  1315. #endif
  1316. ,
  1317. *this,
  1318. lib::forward<That>(that));
  1319. }
  1320. }
  1321. };
  1322. template <typename Traits, Trait = Traits::move_assignable_trait>
  1323. class move_assignment;
  1324. #define MPARK_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, definition) \
  1325. template <typename... Ts> \
  1326. class move_assignment<traits<Ts...>, move_assignable_trait> \
  1327. : public assignment<traits<Ts...>> { \
  1328. using super = assignment<traits<Ts...>>; \
  1329. \
  1330. public: \
  1331. INHERITING_CTOR(move_assignment, super) \
  1332. using super::operator=; \
  1333. \
  1334. move_assignment(const move_assignment &) = default; \
  1335. move_assignment(move_assignment &&) = default; \
  1336. ~move_assignment() = default; \
  1337. move_assignment &operator=(const move_assignment &) = default; \
  1338. definition \
  1339. }
  1340. MPARK_VARIANT_MOVE_ASSIGNMENT(
  1341. Trait::TriviallyAvailable,
  1342. move_assignment &operator=(move_assignment &&that) = default;);
  1343. MPARK_VARIANT_MOVE_ASSIGNMENT(
  1344. Trait::Available,
  1345. move_assignment &
  1346. operator=(move_assignment &&that) noexcept(
  1347. all((std::is_nothrow_move_constructible<Ts>::value &&
  1348. std::is_nothrow_move_assignable<Ts>::value)...)) {
  1349. this->generic_assign(lib::move(that));
  1350. return *this;
  1351. });
  1352. MPARK_VARIANT_MOVE_ASSIGNMENT(
  1353. Trait::Unavailable,
  1354. move_assignment &operator=(move_assignment &&) = delete;);
  1355. #undef MPARK_VARIANT_MOVE_ASSIGNMENT
  1356. template <typename Traits, Trait = Traits::copy_assignable_trait>
  1357. class copy_assignment;
  1358. #define MPARK_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, definition) \
  1359. template <typename... Ts> \
  1360. class copy_assignment<traits<Ts...>, copy_assignable_trait> \
  1361. : public move_assignment<traits<Ts...>> { \
  1362. using super = move_assignment<traits<Ts...>>; \
  1363. \
  1364. public: \
  1365. INHERITING_CTOR(copy_assignment, super) \
  1366. using super::operator=; \
  1367. \
  1368. copy_assignment(const copy_assignment &) = default; \
  1369. copy_assignment(copy_assignment &&) = default; \
  1370. ~copy_assignment() = default; \
  1371. definition \
  1372. copy_assignment &operator=(copy_assignment &&) = default; \
  1373. }
  1374. MPARK_VARIANT_COPY_ASSIGNMENT(
  1375. Trait::TriviallyAvailable,
  1376. copy_assignment &operator=(const copy_assignment &that) = default;);
  1377. MPARK_VARIANT_COPY_ASSIGNMENT(
  1378. Trait::Available,
  1379. copy_assignment &operator=(const copy_assignment &that) {
  1380. this->generic_assign(that);
  1381. return *this;
  1382. });
  1383. MPARK_VARIANT_COPY_ASSIGNMENT(
  1384. Trait::Unavailable,
  1385. copy_assignment &operator=(const copy_assignment &) = delete;);
  1386. #undef MPARK_VARIANT_COPY_ASSIGNMENT
  1387. template <typename... Ts>
  1388. class impl : public copy_assignment<traits<Ts...>> {
  1389. using super = copy_assignment<traits<Ts...>>;
  1390. public:
  1391. INHERITING_CTOR(impl, super)
  1392. using super::operator=;
  1393. template <std::size_t I, typename Arg>
  1394. inline void assign(Arg &&arg) {
  1395. this->assign_alt(access::base::get_alt<I>(*this),
  1396. lib::forward<Arg>(arg));
  1397. }
  1398. inline void swap(impl &that) {
  1399. if (this->valueless_by_exception() && that.valueless_by_exception()) {
  1400. // do nothing.
  1401. } else if (this->index() == that.index()) {
  1402. visitation::base::visit_alt_at(this->index(),
  1403. #ifdef MPARK_GENERIC_LAMBDAS
  1404. [](auto &this_alt, auto &that_alt) {
  1405. using std::swap;
  1406. swap(this_alt.value,
  1407. that_alt.value);
  1408. }
  1409. #else
  1410. swapper{}
  1411. #endif
  1412. ,
  1413. *this,
  1414. that);
  1415. } else {
  1416. impl *lhs = this;
  1417. impl *rhs = lib::addressof(that);
  1418. if (lhs->move_nothrow() && !rhs->move_nothrow()) {
  1419. std::swap(lhs, rhs);
  1420. }
  1421. impl tmp(lib::move(*rhs));
  1422. #ifdef MPARK_EXCEPTIONS
  1423. // EXTENSION: When the move construction of `lhs` into `rhs` throws
  1424. // and `tmp` is nothrow move constructible then we move `tmp` back
  1425. // into `rhs` and provide the strong exception safety guarantee.
  1426. try {
  1427. this->generic_construct(*rhs, lib::move(*lhs));
  1428. } catch (...) {
  1429. if (tmp.move_nothrow()) {
  1430. this->generic_construct(*rhs, lib::move(tmp));
  1431. }
  1432. throw;
  1433. }
  1434. #else
  1435. this->generic_construct(*rhs, lib::move(*lhs));
  1436. #endif
  1437. this->generic_construct(*lhs, lib::move(tmp));
  1438. }
  1439. }
  1440. private:
  1441. #ifndef MPARK_GENERIC_LAMBDAS
  1442. struct swapper {
  1443. template <typename ThisAlt, typename ThatAlt>
  1444. inline void operator()(ThisAlt &this_alt, ThatAlt &that_alt) const {
  1445. using std::swap;
  1446. swap(this_alt.value, that_alt.value);
  1447. }
  1448. };
  1449. #endif
  1450. inline constexpr bool move_nothrow() const {
  1451. return this->valueless_by_exception() ||
  1452. lib::array<bool, sizeof...(Ts)>{
  1453. {std::is_nothrow_move_constructible<Ts>::value...}
  1454. }[this->index()];
  1455. }
  1456. };
  1457. template <typename... Ts>
  1458. struct overload;
  1459. template <>
  1460. struct overload<> { void operator()() const {} };
  1461. template <typename T, typename... Ts>
  1462. struct overload<T, Ts...> : overload<Ts...> {
  1463. using overload<Ts...>::operator();
  1464. lib::identity<T> operator()(T) const { return {}; }
  1465. };
  1466. template <typename T, typename... Ts>
  1467. using best_match_t =
  1468. typename lib::invoke_result_t<overload<Ts...>, T &&>::type;
  1469. } // detail
  1470. template <typename... Ts>
  1471. class variant {
  1472. static_assert(0 < sizeof...(Ts),
  1473. "variant must consist of at least one alternative.");
  1474. static_assert(lib::all<!std::is_array<Ts>::value...>::value,
  1475. "variant can not have an array type as an alternative.");
  1476. static_assert(lib::all<!std::is_reference<Ts>::value...>::value,
  1477. "variant can not have a reference type as an alternative.");
  1478. static_assert(lib::all<!std::is_void<Ts>::value...>::value,
  1479. "variant can not have a void type as an alternative.");
  1480. public:
  1481. template <
  1482. typename Front = lib::type_pack_element_t<0, Ts...>,
  1483. lib::enable_if_t<std::is_default_constructible<Front>::value, int> = 0>
  1484. inline constexpr variant() noexcept(
  1485. std::is_nothrow_default_constructible<Front>::value)
  1486. : impl_(in_place_index_t<0>{}) {}
  1487. variant(const variant &) = default;
  1488. variant(variant &&) = default;
  1489. template <typename Arg,
  1490. lib::enable_if_t<!std::is_same<lib::decay_t<Arg>, variant>::value,
  1491. int> = 0,
  1492. typename T = detail::best_match_t<Arg, Ts...>,
  1493. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1494. lib::enable_if_t<std::is_constructible<T, Arg>::value, int> = 0>
  1495. inline constexpr variant(Arg &&arg) noexcept(
  1496. std::is_nothrow_constructible<T, Arg>::value)
  1497. : impl_(in_place_index_t<I>{}, lib::forward<Arg>(arg)) {}
  1498. template <
  1499. std::size_t I,
  1500. typename... Args,
  1501. typename T = lib::type_pack_element_t<I, Ts...>,
  1502. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1503. inline explicit constexpr variant(
  1504. in_place_index_t<I>,
  1505. Args &&... args) noexcept(std::is_nothrow_constructible<T,
  1506. Args...>::value)
  1507. : impl_(in_place_index_t<I>{}, lib::forward<Args>(args)...) {}
  1508. template <
  1509. std::size_t I,
  1510. typename Up,
  1511. typename... Args,
  1512. typename T = lib::type_pack_element_t<I, Ts...>,
  1513. lib::enable_if_t<std::is_constructible<T,
  1514. std::initializer_list<Up> &,
  1515. Args...>::value,
  1516. int> = 0>
  1517. inline explicit constexpr variant(
  1518. in_place_index_t<I>,
  1519. std::initializer_list<Up> il,
  1520. Args &&... args) noexcept(std::
  1521. is_nothrow_constructible<
  1522. T,
  1523. std::initializer_list<Up> &,
  1524. Args...>::value)
  1525. : impl_(in_place_index_t<I>{}, il, lib::forward<Args>(args)...) {}
  1526. template <
  1527. typename T,
  1528. typename... Args,
  1529. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1530. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1531. inline explicit constexpr variant(
  1532. in_place_type_t<T>,
  1533. Args &&... args) noexcept(std::is_nothrow_constructible<T,
  1534. Args...>::value)
  1535. : impl_(in_place_index_t<I>{}, lib::forward<Args>(args)...) {}
  1536. template <
  1537. typename T,
  1538. typename Up,
  1539. typename... Args,
  1540. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1541. lib::enable_if_t<std::is_constructible<T,
  1542. std::initializer_list<Up> &,
  1543. Args...>::value,
  1544. int> = 0>
  1545. inline explicit constexpr variant(
  1546. in_place_type_t<T>,
  1547. std::initializer_list<Up> il,
  1548. Args &&... args) noexcept(std::
  1549. is_nothrow_constructible<
  1550. T,
  1551. std::initializer_list<Up> &,
  1552. Args...>::value)
  1553. : impl_(in_place_index_t<I>{}, il, lib::forward<Args>(args)...) {}
  1554. ~variant() = default;
  1555. variant &operator=(const variant &) = default;
  1556. variant &operator=(variant &&) = default;
  1557. template <typename Arg,
  1558. lib::enable_if_t<!std::is_same<lib::decay_t<Arg>, variant>::value,
  1559. int> = 0,
  1560. typename T = detail::best_match_t<Arg, Ts...>,
  1561. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1562. lib::enable_if_t<(std::is_assignable<T &, Arg>::value &&
  1563. std::is_constructible<T, Arg>::value),
  1564. int> = 0>
  1565. inline variant &operator=(Arg &&arg) noexcept(
  1566. (std::is_nothrow_assignable<T &, Arg>::value &&
  1567. std::is_nothrow_constructible<T, Arg>::value)) {
  1568. impl_.template assign<I>(lib::forward<Arg>(arg));
  1569. return *this;
  1570. }
  1571. template <
  1572. std::size_t I,
  1573. typename... Args,
  1574. typename T = lib::type_pack_element_t<I, Ts...>,
  1575. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1576. inline T &emplace(Args &&... args) {
  1577. return impl_.template emplace<I>(lib::forward<Args>(args)...);
  1578. }
  1579. template <
  1580. std::size_t I,
  1581. typename Up,
  1582. typename... Args,
  1583. typename T = lib::type_pack_element_t<I, Ts...>,
  1584. lib::enable_if_t<std::is_constructible<T,
  1585. std::initializer_list<Up> &,
  1586. Args...>::value,
  1587. int> = 0>
  1588. inline T &emplace(std::initializer_list<Up> il, Args &&... args) {
  1589. return impl_.template emplace<I>(il, lib::forward<Args>(args)...);
  1590. }
  1591. template <
  1592. typename T,
  1593. typename... Args,
  1594. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1595. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1596. inline T &emplace(Args &&... args) {
  1597. return impl_.template emplace<I>(lib::forward<Args>(args)...);
  1598. }
  1599. template <
  1600. typename T,
  1601. typename Up,
  1602. typename... Args,
  1603. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1604. lib::enable_if_t<std::is_constructible<T,
  1605. std::initializer_list<Up> &,
  1606. Args...>::value,
  1607. int> = 0>
  1608. inline T &emplace(std::initializer_list<Up> il, Args &&... args) {
  1609. return impl_.template emplace<I>(il, lib::forward<Args>(args)...);
  1610. }
  1611. inline constexpr bool valueless_by_exception() const noexcept {
  1612. return impl_.valueless_by_exception();
  1613. }
  1614. inline constexpr std::size_t index() const noexcept {
  1615. return impl_.index();
  1616. }
  1617. template <
  1618. bool Dummy = true,
  1619. lib::enable_if_t<lib::all<Dummy,
  1620. (std::is_move_constructible<Ts>::value &&
  1621. lib::is_swappable<Ts>::value)...>::value,
  1622. int> = 0>
  1623. inline void swap(variant &that) noexcept(
  1624. lib::all<(std::is_nothrow_move_constructible<Ts>::value &&
  1625. lib::is_nothrow_swappable<Ts>::value)...>::value) {
  1626. impl_.swap(that.impl_);
  1627. }
  1628. private:
  1629. detail::impl<Ts...> impl_;
  1630. friend struct detail::access::variant;
  1631. friend struct detail::visitation::variant;
  1632. };
  1633. template <std::size_t I, typename... Ts>
  1634. inline constexpr bool holds_alternative(const variant<Ts...> &v) noexcept {
  1635. return v.index() == I;
  1636. }
  1637. template <typename T, typename... Ts>
  1638. inline constexpr bool holds_alternative(const variant<Ts...> &v) noexcept {
  1639. return holds_alternative<detail::find_index_checked<T, Ts...>::value>(v);
  1640. }
  1641. namespace detail {
  1642. template <std::size_t I, typename V>
  1643. struct generic_get_impl {
  1644. constexpr generic_get_impl(int) {}
  1645. constexpr AUTO_REFREF operator()(V &&v) const
  1646. AUTO_REFREF_RETURN(
  1647. access::variant::get_alt<I>(lib::forward<V>(v)).value)
  1648. };
  1649. template <std::size_t I, typename V>
  1650. inline constexpr AUTO_REFREF generic_get(V &&v)
  1651. AUTO_REFREF_RETURN(generic_get_impl<I, V>(
  1652. holds_alternative<I>(v) ? 0 : (throw_bad_variant_access(), 0))(
  1653. lib::forward<V>(v)))
  1654. } // namespace detail
  1655. template <std::size_t I, typename... Ts>
  1656. inline constexpr variant_alternative_t<I, variant<Ts...>> &get(
  1657. variant<Ts...> &v) {
  1658. return detail::generic_get<I>(v);
  1659. }
  1660. template <std::size_t I, typename... Ts>
  1661. inline constexpr variant_alternative_t<I, variant<Ts...>> &&get(
  1662. variant<Ts...> &&v) {
  1663. return detail::generic_get<I>(lib::move(v));
  1664. }
  1665. template <std::size_t I, typename... Ts>
  1666. inline constexpr const variant_alternative_t<I, variant<Ts...>> &get(
  1667. const variant<Ts...> &v) {
  1668. return detail::generic_get<I>(v);
  1669. }
  1670. template <std::size_t I, typename... Ts>
  1671. inline constexpr const variant_alternative_t<I, variant<Ts...>> &&get(
  1672. const variant<Ts...> &&v) {
  1673. return detail::generic_get<I>(lib::move(v));
  1674. }
  1675. template <typename T, typename... Ts>
  1676. inline constexpr T &get(variant<Ts...> &v) {
  1677. return get<detail::find_index_checked<T, Ts...>::value>(v);
  1678. }
  1679. template <typename T, typename... Ts>
  1680. inline constexpr T &&get(variant<Ts...> &&v) {
  1681. return get<detail::find_index_checked<T, Ts...>::value>(lib::move(v));
  1682. }
  1683. template <typename T, typename... Ts>
  1684. inline constexpr const T &get(const variant<Ts...> &v) {
  1685. return get<detail::find_index_checked<T, Ts...>::value>(v);
  1686. }
  1687. template <typename T, typename... Ts>
  1688. inline constexpr const T &&get(const variant<Ts...> &&v) {
  1689. return get<detail::find_index_checked<T, Ts...>::value>(lib::move(v));
  1690. }
  1691. namespace detail {
  1692. template <std::size_t I, typename V>
  1693. inline constexpr /* auto * */ AUTO generic_get_if(V *v) noexcept
  1694. AUTO_RETURN(v && holds_alternative<I>(*v)
  1695. ? lib::addressof(access::variant::get_alt<I>(*v).value)
  1696. : nullptr)
  1697. } // namespace detail
  1698. template <std::size_t I, typename... Ts>
  1699. inline constexpr lib::add_pointer_t<variant_alternative_t<I, variant<Ts...>>>
  1700. get_if(variant<Ts...> *v) noexcept {
  1701. return detail::generic_get_if<I>(v);
  1702. }
  1703. template <std::size_t I, typename... Ts>
  1704. inline constexpr lib::add_pointer_t<
  1705. const variant_alternative_t<I, variant<Ts...>>>
  1706. get_if(const variant<Ts...> *v) noexcept {
  1707. return detail::generic_get_if<I>(v);
  1708. }
  1709. template <typename T, typename... Ts>
  1710. inline constexpr lib::add_pointer_t<T>
  1711. get_if(variant<Ts...> *v) noexcept {
  1712. return get_if<detail::find_index_checked<T, Ts...>::value>(v);
  1713. }
  1714. template <typename T, typename... Ts>
  1715. inline constexpr lib::add_pointer_t<const T>
  1716. get_if(const variant<Ts...> *v) noexcept {
  1717. return get_if<detail::find_index_checked<T, Ts...>::value>(v);
  1718. }
  1719. template <typename... Ts>
  1720. inline constexpr bool operator==(const variant<Ts...> &lhs,
  1721. const variant<Ts...> &rhs) {
  1722. using detail::visitation::variant;
  1723. using lib::equal_to;
  1724. #ifdef MPARK_CPP14_CONSTEXPR
  1725. if (lhs.index() != rhs.index()) return false;
  1726. if (lhs.valueless_by_exception()) return true;
  1727. return variant::visit_value_at(lhs.index(), equal_to{}, lhs, rhs);
  1728. #else
  1729. return lhs.index() == rhs.index() &&
  1730. (lhs.valueless_by_exception() ||
  1731. variant::visit_value_at(lhs.index(), equal_to{}, lhs, rhs));
  1732. #endif
  1733. }
  1734. template <typename... Ts>
  1735. inline constexpr bool operator!=(const variant<Ts...> &lhs,
  1736. const variant<Ts...> &rhs) {
  1737. using detail::visitation::variant;
  1738. using lib::not_equal_to;
  1739. #ifdef MPARK_CPP14_CONSTEXPR
  1740. if (lhs.index() != rhs.index()) return true;
  1741. if (lhs.valueless_by_exception()) return false;
  1742. return variant::visit_value_at(lhs.index(), not_equal_to{}, lhs, rhs);
  1743. #else
  1744. return lhs.index() != rhs.index() ||
  1745. (!lhs.valueless_by_exception() &&
  1746. variant::visit_value_at(lhs.index(), not_equal_to{}, lhs, rhs));
  1747. #endif
  1748. }
  1749. template <typename... Ts>
  1750. inline constexpr bool operator<(const variant<Ts...> &lhs,
  1751. const variant<Ts...> &rhs) {
  1752. using detail::visitation::variant;
  1753. using lib::less;
  1754. #ifdef MPARK_CPP14_CONSTEXPR
  1755. if (rhs.valueless_by_exception()) return false;
  1756. if (lhs.valueless_by_exception()) return true;
  1757. if (lhs.index() < rhs.index()) return true;
  1758. if (lhs.index() > rhs.index()) return false;
  1759. return variant::visit_value_at(lhs.index(), less{}, lhs, rhs);
  1760. #else
  1761. return !rhs.valueless_by_exception() &&
  1762. (lhs.valueless_by_exception() || lhs.index() < rhs.index() ||
  1763. (lhs.index() == rhs.index() &&
  1764. variant::visit_value_at(lhs.index(), less{}, lhs, rhs)));
  1765. #endif
  1766. }
  1767. template <typename... Ts>
  1768. inline constexpr bool operator>(const variant<Ts...> &lhs,
  1769. const variant<Ts...> &rhs) {
  1770. using detail::visitation::variant;
  1771. using lib::greater;
  1772. #ifdef MPARK_CPP14_CONSTEXPR
  1773. if (lhs.valueless_by_exception()) return false;
  1774. if (rhs.valueless_by_exception()) return true;
  1775. if (lhs.index() > rhs.index()) return true;
  1776. if (lhs.index() < rhs.index()) return false;
  1777. return variant::visit_value_at(lhs.index(), greater{}, lhs, rhs);
  1778. #else
  1779. return !lhs.valueless_by_exception() &&
  1780. (rhs.valueless_by_exception() || lhs.index() > rhs.index() ||
  1781. (lhs.index() == rhs.index() &&
  1782. variant::visit_value_at(lhs.index(), greater{}, lhs, rhs)));
  1783. #endif
  1784. }
  1785. template <typename... Ts>
  1786. inline constexpr bool operator<=(const variant<Ts...> &lhs,
  1787. const variant<Ts...> &rhs) {
  1788. using detail::visitation::variant;
  1789. using lib::less_equal;
  1790. #ifdef MPARK_CPP14_CONSTEXPR
  1791. if (lhs.valueless_by_exception()) return true;
  1792. if (rhs.valueless_by_exception()) return false;
  1793. if (lhs.index() < rhs.index()) return true;
  1794. if (lhs.index() > rhs.index()) return false;
  1795. return variant::visit_value_at(lhs.index(), less_equal{}, lhs, rhs);
  1796. #else
  1797. return lhs.valueless_by_exception() ||
  1798. (!rhs.valueless_by_exception() &&
  1799. (lhs.index() < rhs.index() ||
  1800. (lhs.index() == rhs.index() &&
  1801. variant::visit_value_at(lhs.index(), less_equal{}, lhs, rhs))));
  1802. #endif
  1803. }
  1804. template <typename... Ts>
  1805. inline constexpr bool operator>=(const variant<Ts...> &lhs,
  1806. const variant<Ts...> &rhs) {
  1807. using detail::visitation::variant;
  1808. using lib::greater_equal;
  1809. #ifdef MPARK_CPP14_CONSTEXPR
  1810. if (rhs.valueless_by_exception()) return true;
  1811. if (lhs.valueless_by_exception()) return false;
  1812. if (lhs.index() > rhs.index()) return true;
  1813. if (lhs.index() < rhs.index()) return false;
  1814. return variant::visit_value_at(lhs.index(), greater_equal{}, lhs, rhs);
  1815. #else
  1816. return rhs.valueless_by_exception() ||
  1817. (!lhs.valueless_by_exception() &&
  1818. (lhs.index() > rhs.index() ||
  1819. (lhs.index() == rhs.index() &&
  1820. variant::visit_value_at(
  1821. lhs.index(), greater_equal{}, lhs, rhs))));
  1822. #endif
  1823. }
  1824. template <typename Visitor, typename... Vs>
  1825. inline constexpr DECLTYPE_AUTO visit(Visitor &&visitor, Vs &&... vs)
  1826. DECLTYPE_AUTO_RETURN(
  1827. (detail::all(!vs.valueless_by_exception()...)
  1828. ? (void)0
  1829. : throw_bad_variant_access()),
  1830. detail::visitation::variant::visit_value(lib::forward<Visitor>(visitor),
  1831. lib::forward<Vs>(vs)...))
  1832. struct monostate {};
  1833. inline constexpr bool operator<(monostate, monostate) noexcept {
  1834. return false;
  1835. }
  1836. inline constexpr bool operator>(monostate, monostate) noexcept {
  1837. return false;
  1838. }
  1839. inline constexpr bool operator<=(monostate, monostate) noexcept {
  1840. return true;
  1841. }
  1842. inline constexpr bool operator>=(monostate, monostate) noexcept {
  1843. return true;
  1844. }
  1845. inline constexpr bool operator==(monostate, monostate) noexcept {
  1846. return true;
  1847. }
  1848. inline constexpr bool operator!=(monostate, monostate) noexcept {
  1849. return false;
  1850. }
  1851. template <typename... Ts>
  1852. inline auto swap(variant<Ts...> &lhs,
  1853. variant<Ts...> &rhs) noexcept(noexcept(lhs.swap(rhs)))
  1854. -> decltype(lhs.swap(rhs)) {
  1855. lhs.swap(rhs);
  1856. }
  1857. namespace detail {
  1858. template <typename T, typename...>
  1859. using enabled_type = T;
  1860. namespace hash {
  1861. template <typename H, typename K>
  1862. constexpr bool meets_requirements() {
  1863. return std::is_copy_constructible<H>::value &&
  1864. std::is_move_constructible<H>::value &&
  1865. lib::is_invocable_r<std::size_t, H, const K &>::value;
  1866. }
  1867. template <typename K>
  1868. constexpr bool is_enabled() {
  1869. using H = std::hash<K>;
  1870. return meets_requirements<H, K>() &&
  1871. std::is_default_constructible<H>::value &&
  1872. std::is_copy_assignable<H>::value &&
  1873. std::is_move_assignable<H>::value;
  1874. }
  1875. } // namespace hash
  1876. } // namespace detail
  1877. #undef AUTO
  1878. #undef AUTO_RETURN
  1879. #undef AUTO_REFREF
  1880. #undef AUTO_REFREF_RETURN
  1881. #undef DECLTYPE_AUTO
  1882. #undef DECLTYPE_AUTO_RETURN
  1883. } // namespace mpark
  1884. namespace std {
  1885. template <typename... Ts>
  1886. struct hash<mpark::detail::enabled_type<
  1887. mpark::variant<Ts...>,
  1888. mpark::lib::enable_if_t<mpark::lib::all<mpark::detail::hash::is_enabled<
  1889. mpark::lib::remove_const_t<Ts>>()...>::value>>> {
  1890. using argument_type = mpark::variant<Ts...>;
  1891. using result_type = std::size_t;
  1892. inline result_type operator()(const argument_type &v) const {
  1893. using mpark::detail::visitation::variant;
  1894. std::size_t result =
  1895. v.valueless_by_exception()
  1896. ? 299792458 // Random value chosen by the universe upon creation
  1897. : variant::visit_alt(
  1898. #ifdef MPARK_GENERIC_LAMBDAS
  1899. [](const auto &alt) {
  1900. using alt_type = mpark::lib::decay_t<decltype(alt)>;
  1901. using value_type = mpark::lib::remove_const_t<
  1902. typename alt_type::value_type>;
  1903. return hash<value_type>{}(alt.value);
  1904. }
  1905. #else
  1906. hasher{}
  1907. #endif
  1908. ,
  1909. v);
  1910. return hash_combine(result, hash<std::size_t>{}(v.index()));
  1911. }
  1912. private:
  1913. #ifndef MPARK_GENERIC_LAMBDAS
  1914. struct hasher {
  1915. template <typename Alt>
  1916. inline std::size_t operator()(const Alt &alt) const {
  1917. using alt_type = mpark::lib::decay_t<Alt>;
  1918. using value_type =
  1919. mpark::lib::remove_const_t<typename alt_type::value_type>;
  1920. return hash<value_type>{}(alt.value);
  1921. }
  1922. };
  1923. #endif
  1924. static std::size_t hash_combine(std::size_t lhs, std::size_t rhs) {
  1925. return lhs ^= rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2);
  1926. }
  1927. };
  1928. template <>
  1929. struct hash<mpark::monostate> {
  1930. using argument_type = mpark::monostate;
  1931. using result_type = std::size_t;
  1932. inline result_type operator()(const argument_type &) const noexcept {
  1933. return 66740831; // return a fundamentally attractive random value.
  1934. }
  1935. };
  1936. } // namespace std
  1937. #endif // MPARK_VARIANT_HPP