variant.hpp 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819
  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 < 190024210)
  158. #error "MPark.Variant requires C++11 support."
  159. #endif
  160. #ifndef __has_attribute
  161. #define __has_attribute(x) 0
  162. #endif
  163. #ifndef __has_builtin
  164. #define __has_builtin(x) 0
  165. #endif
  166. #ifndef __has_include
  167. #define __has_include(x) 0
  168. #endif
  169. #ifndef __has_feature
  170. #define __has_feature(x) 0
  171. #endif
  172. #if __has_attribute(always_inline) || defined(__GNUC__)
  173. #define MPARK_ALWAYS_INLINE __attribute__((__always_inline__)) inline
  174. #elif defined(_MSC_VER)
  175. #define MPARK_ALWAYS_INLINE __forceinline
  176. #else
  177. #define MPARK_ALWAYS_INLINE inline
  178. #endif
  179. #if __has_builtin(__builtin_addressof) || \
  180. (defined(__GNUC__) && __GNUC__ >= 7) || defined(_MSC_VER)
  181. #define MPARK_BUILTIN_ADDRESSOF
  182. #endif
  183. #if __has_builtin(__builtin_unreachable) || defined(__GNUC__)
  184. #define MPARK_BUILTIN_UNREACHABLE __builtin_unreachable()
  185. #elif defined(_MSC_VER)
  186. #define MPARK_BUILTIN_UNREACHABLE __assume(false)
  187. #else
  188. #define MPARK_BUILTIN_UNREACHABLE
  189. #endif
  190. #if __has_builtin(__type_pack_element)
  191. #define MPARK_TYPE_PACK_ELEMENT
  192. #endif
  193. #if defined(__cpp_constexpr) && __cpp_constexpr >= 200704 && \
  194. !(defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 9)
  195. #define MPARK_CPP11_CONSTEXPR
  196. #endif
  197. #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304
  198. #define MPARK_CPP14_CONSTEXPR
  199. #endif
  200. #if __has_feature(cxx_exceptions) || defined(__cpp_exceptions) || \
  201. (defined(_MSC_VER) && defined(_CPPUNWIND))
  202. #define MPARK_EXCEPTIONS
  203. #endif
  204. #if defined(__cpp_generic_lambdas) || defined(_MSC_VER)
  205. #define MPARK_GENERIC_LAMBDAS
  206. #endif
  207. #if defined(__cpp_lib_integer_sequence)
  208. #define MPARK_INTEGER_SEQUENCE
  209. #endif
  210. #if defined(__cpp_return_type_deduction) || defined(_MSC_VER)
  211. #define MPARK_RETURN_TYPE_DEDUCTION
  212. #endif
  213. #if defined(__cpp_lib_transparent_operators) || defined(_MSC_VER)
  214. #define MPARK_TRANSPARENT_OPERATORS
  215. #endif
  216. #if defined(__cpp_variable_templates) || defined(_MSC_VER)
  217. #define MPARK_VARIABLE_TEMPLATES
  218. #endif
  219. #if !defined(__GLIBCXX__) || __has_include(<codecvt>) // >= libstdc++-5
  220. #define MPARK_TRIVIALITY_TYPE_TRAITS
  221. #define MPARK_INCOMPLETE_TYPE_TRAITS
  222. #endif
  223. #endif // MPARK_CONFIG_HPP
  224. // MPark.Variant
  225. //
  226. // Copyright Michael Park, 2015-2017
  227. //
  228. // Distributed under the Boost Software License, Version 1.0.
  229. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  230. #ifndef MPARK_IN_PLACE_HPP
  231. #define MPARK_IN_PLACE_HPP
  232. #include <cstddef>
  233. namespace mpark {
  234. struct in_place_t { explicit in_place_t() = default; };
  235. template <std::size_t I>
  236. struct in_place_index_t { explicit in_place_index_t() = default; };
  237. template <typename T>
  238. struct in_place_type_t { explicit in_place_type_t() = default; };
  239. #ifdef MPARK_VARIABLE_TEMPLATES
  240. constexpr in_place_t in_place{};
  241. template <std::size_t I> constexpr in_place_index_t<I> in_place_index{};
  242. template <typename T> constexpr in_place_type_t<T> in_place_type{};
  243. #endif
  244. } // namespace mpark
  245. #endif // MPARK_IN_PLACE_HPP
  246. // MPark.Variant
  247. //
  248. // Copyright Michael Park, 2015-2017
  249. //
  250. // Distributed under the Boost Software License, Version 1.0.
  251. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  252. #ifndef MPARK_LIB_HPP
  253. #define MPARK_LIB_HPP
  254. #include <memory>
  255. #include <functional>
  256. #include <type_traits>
  257. #include <utility>
  258. #define MPARK_RETURN(...) \
  259. noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) { return __VA_ARGS__; }
  260. namespace mpark {
  261. namespace lib {
  262. template <typename T>
  263. struct identity { using type = T; };
  264. inline namespace cpp14 {
  265. template <typename T, std::size_t N>
  266. struct array {
  267. constexpr const T &operator[](std::size_t index) const {
  268. return data[index];
  269. }
  270. T data[N == 0 ? 1 : N];
  271. };
  272. template <typename T>
  273. using add_pointer_t = typename std::add_pointer<T>::type;
  274. template <typename... Ts>
  275. using common_type_t = typename std::common_type<Ts...>::type;
  276. template <typename T>
  277. using decay_t = typename std::decay<T>::type;
  278. template <bool B, typename T = void>
  279. using enable_if_t = typename std::enable_if<B, T>::type;
  280. template <typename T>
  281. using remove_const_t = typename std::remove_const<T>::type;
  282. template <typename T>
  283. using remove_reference_t = typename std::remove_reference<T>::type;
  284. template <typename T>
  285. inline constexpr T &&forward(remove_reference_t<T> &t) noexcept {
  286. return static_cast<T &&>(t);
  287. }
  288. template <typename T>
  289. inline constexpr T &&forward(remove_reference_t<T> &&t) noexcept {
  290. static_assert(!std::is_lvalue_reference<T>::value,
  291. "can not forward an rvalue as an lvalue");
  292. return static_cast<T &&>(t);
  293. }
  294. template <typename T>
  295. inline constexpr remove_reference_t<T> &&move(T &&t) noexcept {
  296. return static_cast<remove_reference_t<T> &&>(t);
  297. }
  298. #ifdef MPARK_INTEGER_SEQUENCE
  299. using std::integer_sequence;
  300. using std::index_sequence;
  301. using std::make_index_sequence;
  302. using std::index_sequence_for;
  303. #else
  304. template <typename T, T... Is>
  305. struct integer_sequence {
  306. using value_type = T;
  307. static constexpr std::size_t size() noexcept { return sizeof...(Is); }
  308. };
  309. template <std::size_t... Is>
  310. using index_sequence = integer_sequence<std::size_t, Is...>;
  311. template <typename Lhs, typename Rhs>
  312. struct make_index_sequence_concat;
  313. template <std::size_t... Lhs, std::size_t... Rhs>
  314. struct make_index_sequence_concat<index_sequence<Lhs...>,
  315. index_sequence<Rhs...>>
  316. : identity<index_sequence<Lhs..., (sizeof...(Lhs) + Rhs)...>> {};
  317. template <std::size_t N>
  318. struct make_index_sequence_impl;
  319. template <std::size_t N>
  320. using make_index_sequence = typename make_index_sequence_impl<N>::type;
  321. template <std::size_t N>
  322. struct make_index_sequence_impl
  323. : make_index_sequence_concat<make_index_sequence<N / 2>,
  324. make_index_sequence<N - (N / 2)>> {};
  325. template <>
  326. struct make_index_sequence_impl<0> : identity<index_sequence<>> {};
  327. template <>
  328. struct make_index_sequence_impl<1> : identity<index_sequence<0>> {};
  329. template <typename... Ts>
  330. using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
  331. #endif
  332. // <functional>
  333. #ifdef MPARK_TRANSPARENT_OPERATORS
  334. using equal_to = std::equal_to<>;
  335. #else
  336. struct equal_to {
  337. template <typename Lhs, typename Rhs>
  338. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  339. MPARK_RETURN(lib::forward<Lhs>(lhs) == lib::forward<Rhs>(rhs))
  340. };
  341. #endif
  342. #ifdef MPARK_TRANSPARENT_OPERATORS
  343. using not_equal_to = std::not_equal_to<>;
  344. #else
  345. struct not_equal_to {
  346. template <typename Lhs, typename Rhs>
  347. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  348. MPARK_RETURN(lib::forward<Lhs>(lhs) != lib::forward<Rhs>(rhs))
  349. };
  350. #endif
  351. #ifdef MPARK_TRANSPARENT_OPERATORS
  352. using less = std::less<>;
  353. #else
  354. struct less {
  355. template <typename Lhs, typename Rhs>
  356. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  357. MPARK_RETURN(lib::forward<Lhs>(lhs) < lib::forward<Rhs>(rhs))
  358. };
  359. #endif
  360. #ifdef MPARK_TRANSPARENT_OPERATORS
  361. using greater = std::greater<>;
  362. #else
  363. struct greater {
  364. template <typename Lhs, typename Rhs>
  365. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  366. MPARK_RETURN(lib::forward<Lhs>(lhs) > lib::forward<Rhs>(rhs))
  367. };
  368. #endif
  369. #ifdef MPARK_TRANSPARENT_OPERATORS
  370. using less_equal = std::less_equal<>;
  371. #else
  372. struct less_equal {
  373. template <typename Lhs, typename Rhs>
  374. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  375. MPARK_RETURN(lib::forward<Lhs>(lhs) <= lib::forward<Rhs>(rhs))
  376. };
  377. #endif
  378. #ifdef MPARK_TRANSPARENT_OPERATORS
  379. using greater_equal = std::greater_equal<>;
  380. #else
  381. struct greater_equal {
  382. template <typename Lhs, typename Rhs>
  383. inline constexpr auto operator()(Lhs &&lhs, Rhs &&rhs) const
  384. MPARK_RETURN(lib::forward<Lhs>(lhs) >= lib::forward<Rhs>(rhs))
  385. };
  386. #endif
  387. } // namespace cpp14
  388. inline namespace cpp17 {
  389. // <type_traits>
  390. template <bool B>
  391. using bool_constant = std::integral_constant<bool, B>;
  392. template <typename...>
  393. struct voider : identity<void> {};
  394. template <typename... Ts>
  395. using void_t = typename voider<Ts...>::type;
  396. namespace detail {
  397. namespace swappable {
  398. using std::swap;
  399. template <typename T>
  400. struct is_swappable {
  401. private:
  402. template <typename U,
  403. typename = decltype(swap(std::declval<U &>(),
  404. std::declval<U &>()))>
  405. inline static std::true_type test(int);
  406. template <typename U>
  407. inline static std::false_type test(...);
  408. public:
  409. static constexpr bool value = decltype(test<T>(0))::value;
  410. };
  411. template <bool IsSwappable, typename T>
  412. struct is_nothrow_swappable {
  413. static constexpr bool value =
  414. noexcept(swap(std::declval<T &>(), std::declval<T &>()));
  415. };
  416. template <typename T>
  417. struct is_nothrow_swappable<false, T> : std::false_type {};
  418. } // namespace swappable
  419. } // namespace detail
  420. using detail::swappable::is_swappable;
  421. template <typename T>
  422. using is_nothrow_swappable =
  423. detail::swappable::is_nothrow_swappable<is_swappable<T>::value, T>;
  424. // <functional>
  425. namespace detail {
  426. template <typename T>
  427. struct is_reference_wrapper : std::false_type {};
  428. template <typename T>
  429. struct is_reference_wrapper<std::reference_wrapper<T>>
  430. : std::true_type {};
  431. template <bool, int>
  432. struct Invoke;
  433. template <>
  434. struct Invoke<true /* pmf */, 0 /* is_base_of */> {
  435. template <typename R, typename T, typename Arg, typename... Args>
  436. inline static constexpr auto invoke(R T::*pmf, Arg &&arg, Args &&... args)
  437. MPARK_RETURN((lib::forward<Arg>(arg).*pmf)(lib::forward<Args>(args)...))
  438. };
  439. template <>
  440. struct Invoke<true /* pmf */, 1 /* is_reference_wrapper */> {
  441. template <typename R, typename T, typename Arg, typename... Args>
  442. inline static constexpr auto invoke(R T::*pmf, Arg &&arg, Args &&... args)
  443. MPARK_RETURN((lib::forward<Arg>(arg).get().*pmf)(lib::forward<Args>(args)...))
  444. };
  445. template <>
  446. struct Invoke<true /* pmf */, 2 /* otherwise */> {
  447. template <typename R, typename T, typename Arg, typename... Args>
  448. inline static constexpr auto invoke(R T::*pmf, Arg &&arg, Args &&... args)
  449. MPARK_RETURN(((*lib::forward<Arg>(arg)).*pmf)(lib::forward<Args>(args)...))
  450. };
  451. template <>
  452. struct Invoke<false /* pmo */, 0 /* is_base_of */> {
  453. template <typename R, typename T, typename Arg>
  454. inline static constexpr auto invoke(R T::*pmo, Arg &&arg)
  455. MPARK_RETURN(lib::forward<Arg>(arg).*pmo)
  456. };
  457. template <>
  458. struct Invoke<false /* pmo */, 1 /* is_reference_wrapper */> {
  459. template <typename R, typename T, typename Arg>
  460. inline static constexpr auto invoke(R T::*pmo, Arg &&arg)
  461. MPARK_RETURN(lib::forward<Arg>(arg).get().*pmo)
  462. };
  463. template <>
  464. struct Invoke<false /* pmo */, 2 /* otherwise */> {
  465. template <typename R, typename T, typename Arg>
  466. inline static constexpr auto invoke(R T::*pmo, Arg &&arg)
  467. MPARK_RETURN((*lib::forward<Arg>(arg)).*pmo)
  468. };
  469. template <typename R, typename T, typename Arg, typename... Args>
  470. inline constexpr auto invoke(R T::*f, Arg &&arg, Args &&... args)
  471. MPARK_RETURN(
  472. Invoke<std::is_function<R>::value,
  473. (std::is_base_of<T, lib::decay_t<Arg>>::value
  474. ? 0
  475. : is_reference_wrapper<lib::decay_t<Arg>>::value
  476. ? 1
  477. : 2)>::invoke(f,
  478. lib::forward<Arg>(arg),
  479. lib::forward<Args>(args)...))
  480. #ifdef _MSC_VER
  481. #pragma warning(push)
  482. #pragma warning(disable : 4100)
  483. #endif
  484. template <typename F, typename... Args>
  485. inline constexpr auto invoke(F &&f, Args &&... args)
  486. MPARK_RETURN(lib::forward<F>(f)(lib::forward<Args>(args)...))
  487. #ifdef _MSC_VER
  488. #pragma warning(pop)
  489. #endif
  490. } // namespace detail
  491. template <typename F, typename... Args>
  492. inline constexpr auto invoke(F &&f, Args &&... args)
  493. MPARK_RETURN(detail::invoke(lib::forward<F>(f),
  494. lib::forward<Args>(args)...))
  495. namespace detail {
  496. template <typename Void, typename, typename...>
  497. struct invoke_result {};
  498. template <typename F, typename... Args>
  499. struct invoke_result<void_t<decltype(lib::invoke(
  500. std::declval<F>(), std::declval<Args>()...))>,
  501. F,
  502. Args...>
  503. : identity<decltype(
  504. lib::invoke(std::declval<F>(), std::declval<Args>()...))> {};
  505. } // namespace detail
  506. template <typename F, typename... Args>
  507. using invoke_result = detail::invoke_result<void, F, Args...>;
  508. template <typename F, typename... Args>
  509. using invoke_result_t = typename invoke_result<F, Args...>::type;
  510. namespace detail {
  511. template <typename Void, typename, typename...>
  512. struct is_invocable : std::false_type {};
  513. template <typename F, typename... Args>
  514. struct is_invocable<void_t<invoke_result_t<F, Args...>>, F, Args...>
  515. : std::true_type {};
  516. template <typename Void, typename, typename, typename...>
  517. struct is_invocable_r : std::false_type {};
  518. template <typename R, typename F, typename... Args>
  519. struct is_invocable_r<void_t<invoke_result_t<F, Args...>>,
  520. R,
  521. F,
  522. Args...>
  523. : std::is_convertible<invoke_result_t<F, Args...>, R> {};
  524. } // namespace detail
  525. template <typename F, typename... Args>
  526. using is_invocable = detail::is_invocable<void, F, Args...>;
  527. template <typename R, typename F, typename... Args>
  528. using is_invocable_r = detail::is_invocable_r<void, R, F, Args...>;
  529. namespace detail {
  530. template <bool Invocable, typename F, typename... Args>
  531. struct is_nothrow_invocable {
  532. static constexpr bool value =
  533. noexcept(lib::invoke(std::declval<F>(), std::declval<Args>()...));
  534. };
  535. template <typename F, typename... Args>
  536. struct is_nothrow_invocable<false, F, Args...> : std::false_type {};
  537. template <bool Invocable, typename R, typename F, typename... Args>
  538. struct is_nothrow_invocable_r {
  539. private:
  540. inline static R impl() {
  541. return lib::invoke(std::declval<F>(), std::declval<Args>()...);
  542. }
  543. public:
  544. static constexpr bool value = noexcept(impl());
  545. };
  546. template <typename R, typename F, typename... Args>
  547. struct is_nothrow_invocable_r<false, R, F, Args...> : std::false_type {};
  548. } // namespace detail
  549. template <typename F, typename... Args>
  550. using is_nothrow_invocable = detail::
  551. is_nothrow_invocable<is_invocable<F, Args...>::value, F, Args...>;
  552. template <typename R, typename F, typename... Args>
  553. using is_nothrow_invocable_r =
  554. detail::is_nothrow_invocable_r<is_invocable_r<R, F, Args...>::value,
  555. R,
  556. F,
  557. Args...>;
  558. // <memory>
  559. #ifdef MPARK_BUILTIN_ADDRESSOF
  560. template <typename T>
  561. inline constexpr T *addressof(T &arg) noexcept {
  562. return __builtin_addressof(arg);
  563. }
  564. #else
  565. namespace detail {
  566. namespace has_addressof_impl {
  567. struct fail;
  568. template <typename T>
  569. inline fail operator&(T &&);
  570. template <typename T>
  571. inline static constexpr bool impl() {
  572. return (std::is_class<T>::value || std::is_union<T>::value) &&
  573. !std::is_same<decltype(&std::declval<T &>()), fail>::value;
  574. }
  575. } // namespace has_addressof_impl
  576. template <typename T>
  577. using has_addressof = bool_constant<has_addressof_impl::impl<T>()>;
  578. template <typename T>
  579. inline constexpr T *addressof(T &arg, std::true_type) noexcept {
  580. return std::addressof(arg);
  581. }
  582. template <typename T>
  583. inline constexpr T *addressof(T &arg, std::false_type) noexcept {
  584. return &arg;
  585. }
  586. } // namespace detail
  587. template <typename T>
  588. inline constexpr T *addressof(T &arg) noexcept {
  589. return detail::addressof(arg, detail::has_addressof<T>{});
  590. }
  591. #endif
  592. template <typename T>
  593. inline constexpr T *addressof(const T &&) = delete;
  594. } // namespace cpp17
  595. template <typename T>
  596. struct remove_all_extents : identity<T> {};
  597. template <typename T, std::size_t N>
  598. struct remove_all_extents<array<T, N>> : remove_all_extents<T> {};
  599. template <typename T>
  600. using remove_all_extents_t = typename remove_all_extents<T>::type;
  601. template <std::size_t N>
  602. using size_constant = std::integral_constant<std::size_t, N>;
  603. template <std::size_t I, typename T>
  604. struct indexed_type : size_constant<I> { using type = T; };
  605. template <bool... Bs>
  606. using all = std::is_same<integer_sequence<bool, true, Bs...>,
  607. integer_sequence<bool, Bs..., true>>;
  608. #ifdef MPARK_TYPE_PACK_ELEMENT
  609. template <std::size_t I, typename... Ts>
  610. using type_pack_element_t = __type_pack_element<I, Ts...>;
  611. #else
  612. template <std::size_t I, typename... Ts>
  613. struct type_pack_element_impl {
  614. private:
  615. template <typename>
  616. struct set;
  617. template <std::size_t... Is>
  618. struct set<index_sequence<Is...>> : indexed_type<Is, Ts>... {};
  619. template <typename T>
  620. inline static std::enable_if<true, T> impl(indexed_type<I, T>);
  621. inline static std::enable_if<false> impl(...);
  622. public:
  623. using type = decltype(impl(set<index_sequence_for<Ts...>>{}));
  624. };
  625. template <std::size_t I, typename... Ts>
  626. using type_pack_element = typename type_pack_element_impl<I, Ts...>::type;
  627. template <std::size_t I, typename... Ts>
  628. using type_pack_element_t = typename type_pack_element<I, Ts...>::type;
  629. #endif
  630. #ifdef MPARK_TRIVIALITY_TYPE_TRAITS
  631. using std::is_trivially_copy_constructible;
  632. using std::is_trivially_move_constructible;
  633. using std::is_trivially_copy_assignable;
  634. using std::is_trivially_move_assignable;
  635. #else
  636. template <typename T>
  637. struct is_trivially_copy_constructible
  638. : bool_constant<
  639. std::is_copy_constructible<T>::value && __has_trivial_copy(T)> {};
  640. template <typename T>
  641. struct is_trivially_move_constructible : bool_constant<__is_trivial(T)> {};
  642. template <typename T>
  643. struct is_trivially_copy_assignable
  644. : bool_constant<
  645. std::is_copy_assignable<T>::value && __has_trivial_assign(T)> {};
  646. template <typename T>
  647. struct is_trivially_move_assignable : bool_constant<__is_trivial(T)> {};
  648. #endif
  649. template <typename T, bool>
  650. struct dependent_type : T {};
  651. template <typename Is, std::size_t J>
  652. struct push_back;
  653. template <typename Is, std::size_t J>
  654. using push_back_t = typename push_back<Is, J>::type;
  655. template <std::size_t... Is, std::size_t J>
  656. struct push_back<index_sequence<Is...>, J> {
  657. using type = index_sequence<Is..., J>;
  658. };
  659. } // namespace lib
  660. } // namespace mpark
  661. #undef MPARK_RETURN
  662. #endif // MPARK_LIB_HPP
  663. namespace mpark {
  664. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  665. #define AUTO auto
  666. #define AUTO_RETURN(...) { return __VA_ARGS__; }
  667. #define AUTO_REFREF auto &&
  668. #define AUTO_REFREF_RETURN(...) { return __VA_ARGS__; }
  669. #define DECLTYPE_AUTO decltype(auto)
  670. #define DECLTYPE_AUTO_RETURN(...) { return __VA_ARGS__; }
  671. #else
  672. #define AUTO auto
  673. #define AUTO_RETURN(...) \
  674. -> lib::decay_t<decltype(__VA_ARGS__)> { return __VA_ARGS__; }
  675. #define AUTO_REFREF auto
  676. #define AUTO_REFREF_RETURN(...) \
  677. -> decltype((__VA_ARGS__)) { \
  678. static_assert(std::is_reference<decltype((__VA_ARGS__))>::value, ""); \
  679. return __VA_ARGS__; \
  680. }
  681. #define DECLTYPE_AUTO auto
  682. #define DECLTYPE_AUTO_RETURN(...) \
  683. -> decltype(__VA_ARGS__) { return __VA_ARGS__; }
  684. #endif
  685. class bad_variant_access : public std::exception {
  686. public:
  687. virtual const char *what() const noexcept override { return "bad_variant_access"; }
  688. };
  689. [[noreturn]] inline void throw_bad_variant_access() {
  690. #ifdef MPARK_EXCEPTIONS
  691. throw bad_variant_access{};
  692. #else
  693. std::terminate();
  694. MPARK_BUILTIN_UNREACHABLE;
  695. #endif
  696. }
  697. template <typename... Ts>
  698. class variant;
  699. template <typename T>
  700. struct variant_size;
  701. #ifdef MPARK_VARIABLE_TEMPLATES
  702. template <typename T>
  703. constexpr std::size_t variant_size_v = variant_size<T>::value;
  704. #endif
  705. template <typename T>
  706. struct variant_size<const T> : variant_size<T> {};
  707. template <typename T>
  708. struct variant_size<volatile T> : variant_size<T> {};
  709. template <typename T>
  710. struct variant_size<const volatile T> : variant_size<T> {};
  711. template <typename... Ts>
  712. struct variant_size<variant<Ts...>> : lib::size_constant<sizeof...(Ts)> {};
  713. template <std::size_t I, typename T>
  714. struct variant_alternative;
  715. template <std::size_t I, typename T>
  716. using variant_alternative_t = typename variant_alternative<I, T>::type;
  717. template <std::size_t I, typename T>
  718. struct variant_alternative<I, const T>
  719. : std::add_const<variant_alternative_t<I, T>> {};
  720. template <std::size_t I, typename T>
  721. struct variant_alternative<I, volatile T>
  722. : std::add_volatile<variant_alternative_t<I, T>> {};
  723. template <std::size_t I, typename T>
  724. struct variant_alternative<I, const volatile T>
  725. : std::add_cv<variant_alternative_t<I, T>> {};
  726. template <std::size_t I, typename... Ts>
  727. struct variant_alternative<I, variant<Ts...>> {
  728. static_assert(I < sizeof...(Ts),
  729. "index out of bounds in `std::variant_alternative<>`");
  730. using type = lib::type_pack_element_t<I, Ts...>;
  731. };
  732. constexpr std::size_t variant_npos = static_cast<std::size_t>(-1);
  733. namespace detail {
  734. constexpr std::size_t not_found = static_cast<std::size_t>(-1);
  735. constexpr std::size_t ambiguous = static_cast<std::size_t>(-2);
  736. #ifdef MPARK_CPP14_CONSTEXPR
  737. template <typename T, typename... Ts>
  738. inline constexpr std::size_t find_index() {
  739. constexpr lib::array<bool, sizeof...(Ts)> matches = {
  740. {std::is_same<T, Ts>::value...}
  741. };
  742. std::size_t result = not_found;
  743. for (std::size_t i = 0; i < sizeof...(Ts); ++i) {
  744. if (matches[i]) {
  745. if (result != not_found) {
  746. return ambiguous;
  747. }
  748. result = i;
  749. }
  750. }
  751. return result;
  752. }
  753. #else
  754. inline constexpr std::size_t find_index_impl(std::size_t result,
  755. std::size_t) {
  756. return result;
  757. }
  758. template <typename... Bs>
  759. inline constexpr std::size_t find_index_impl(std::size_t result,
  760. std::size_t idx,
  761. bool b,
  762. Bs... bs) {
  763. return b ? (result != not_found ? ambiguous
  764. : find_index_impl(idx, idx + 1, bs...))
  765. : find_index_impl(result, idx + 1, bs...);
  766. }
  767. template <typename T, typename... Ts>
  768. inline constexpr std::size_t find_index() {
  769. return find_index_impl(not_found, 0, std::is_same<T, Ts>::value...);
  770. }
  771. #endif
  772. template <std::size_t I>
  773. using find_index_sfinae_impl =
  774. lib::enable_if_t<I != not_found && I != ambiguous,
  775. lib::size_constant<I>>;
  776. template <typename T, typename... Ts>
  777. using find_index_sfinae = find_index_sfinae_impl<find_index<T, Ts...>()>;
  778. template <std::size_t I>
  779. struct find_index_checked_impl : lib::size_constant<I> {
  780. static_assert(I != not_found, "the specified type is not found.");
  781. static_assert(I != ambiguous, "the specified type is ambiguous.");
  782. };
  783. template <typename T, typename... Ts>
  784. using find_index_checked = find_index_checked_impl<find_index<T, Ts...>()>;
  785. struct valueless_t {};
  786. enum class Trait { TriviallyAvailable, Available, Unavailable };
  787. template <typename T,
  788. template <typename> class IsTriviallyAvailable,
  789. template <typename> class IsAvailable>
  790. inline constexpr Trait trait() {
  791. return IsTriviallyAvailable<T>::value
  792. ? Trait::TriviallyAvailable
  793. : IsAvailable<T>::value ? Trait::Available
  794. : Trait::Unavailable;
  795. }
  796. #ifdef MPARK_CPP14_CONSTEXPR
  797. template <typename... Traits>
  798. inline constexpr Trait common_trait(Traits... traits_) {
  799. Trait result = Trait::TriviallyAvailable;
  800. lib::array<Trait, sizeof...(Traits)> traits = {{traits_...}};
  801. for (std::size_t i = 0; i < sizeof...(Traits); ++i) {
  802. Trait t = traits[i];
  803. if (static_cast<int>(t) > static_cast<int>(result)) {
  804. result = t;
  805. }
  806. }
  807. return result;
  808. }
  809. #else
  810. inline constexpr Trait common_trait_impl(Trait result) { return result; }
  811. template <typename... Traits>
  812. inline constexpr Trait common_trait_impl(Trait result,
  813. Trait t,
  814. Traits... ts) {
  815. return static_cast<int>(t) > static_cast<int>(result)
  816. ? common_trait_impl(t, ts...)
  817. : common_trait_impl(result, ts...);
  818. }
  819. template <typename... Traits>
  820. inline constexpr Trait common_trait(Traits... ts) {
  821. return common_trait_impl(Trait::TriviallyAvailable, ts...);
  822. }
  823. #endif
  824. template <typename... Ts>
  825. struct traits {
  826. static constexpr Trait copy_constructible_trait =
  827. common_trait(trait<Ts,
  828. lib::is_trivially_copy_constructible,
  829. std::is_copy_constructible>()...);
  830. static constexpr Trait move_constructible_trait =
  831. common_trait(trait<Ts,
  832. lib::is_trivially_move_constructible,
  833. std::is_move_constructible>()...);
  834. static constexpr Trait copy_assignable_trait =
  835. common_trait(copy_constructible_trait,
  836. trait<Ts,
  837. lib::is_trivially_copy_assignable,
  838. std::is_copy_assignable>()...);
  839. static constexpr Trait move_assignable_trait =
  840. common_trait(move_constructible_trait,
  841. trait<Ts,
  842. lib::is_trivially_move_assignable,
  843. std::is_move_assignable>()...);
  844. static constexpr Trait destructible_trait =
  845. common_trait(trait<Ts,
  846. std::is_trivially_destructible,
  847. std::is_destructible>()...);
  848. };
  849. namespace access {
  850. struct recursive_union {
  851. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  852. template <typename V>
  853. inline static constexpr auto &&get_alt(V &&v, in_place_index_t<0>) {
  854. return lib::forward<V>(v).head_;
  855. }
  856. template <typename V, std::size_t I>
  857. inline static constexpr auto &&get_alt(V &&v, in_place_index_t<I>) {
  858. return get_alt(lib::forward<V>(v).tail_, in_place_index_t<I - 1>{});
  859. }
  860. #else
  861. template <std::size_t I, bool Dummy = true>
  862. struct get_alt_impl {
  863. template <typename V>
  864. inline constexpr AUTO_REFREF operator()(V &&v) const
  865. AUTO_REFREF_RETURN(get_alt_impl<I - 1>{}(lib::forward<V>(v).tail_))
  866. };
  867. template <bool Dummy>
  868. struct get_alt_impl<0, Dummy> {
  869. template <typename V>
  870. inline constexpr AUTO_REFREF operator()(V &&v) const
  871. AUTO_REFREF_RETURN(lib::forward<V>(v).head_)
  872. };
  873. template <typename V, std::size_t I>
  874. inline static constexpr AUTO_REFREF get_alt(V &&v, in_place_index_t<I>)
  875. AUTO_REFREF_RETURN(get_alt_impl<I>{}(lib::forward<V>(v)))
  876. #endif
  877. };
  878. struct base {
  879. template <std::size_t I, typename V>
  880. inline static constexpr AUTO_REFREF get_alt(V &&v)
  881. #ifdef _MSC_VER
  882. AUTO_REFREF_RETURN(recursive_union::get_alt(
  883. lib::forward<V>(v).data_, in_place_index_t<I>{}))
  884. #else
  885. AUTO_REFREF_RETURN(recursive_union::get_alt(
  886. data(lib::forward<V>(v)), in_place_index_t<I>{}))
  887. #endif
  888. };
  889. struct variant {
  890. template <std::size_t I, typename V>
  891. inline static constexpr AUTO_REFREF get_alt(V &&v)
  892. AUTO_REFREF_RETURN(base::get_alt<I>(lib::forward<V>(v).impl_))
  893. };
  894. } // namespace access
  895. namespace visitation {
  896. #if defined(MPARK_CPP14_CONSTEXPR) && !defined(_MSC_VER)
  897. #define MPARK_VARIANT_SWITCH_VISIT
  898. #endif
  899. struct base {
  900. template <typename Visitor, typename... Vs>
  901. using dispatch_result_t = decltype(
  902. lib::invoke(std::declval<Visitor>(),
  903. access::base::get_alt<0>(std::declval<Vs>())...));
  904. template <typename Expected>
  905. struct expected {
  906. template <typename Actual>
  907. inline static constexpr bool but_got() {
  908. return std::is_same<Expected, Actual>::value;
  909. }
  910. };
  911. template <typename Expected, typename Actual>
  912. struct visit_return_type_check {
  913. static_assert(
  914. expected<Expected>::template but_got<Actual>(),
  915. "`visit` requires the visitor to have a single return type");
  916. template <typename Visitor, typename... Alts>
  917. inline static constexpr DECLTYPE_AUTO invoke(Visitor &&visitor,
  918. Alts &&... alts)
  919. DECLTYPE_AUTO_RETURN(lib::invoke(lib::forward<Visitor>(visitor),
  920. lib::forward<Alts>(alts)...))
  921. };
  922. #ifdef MPARK_VARIANT_SWITCH_VISIT
  923. template <bool B, typename R, typename... ITs>
  924. struct dispatcher;
  925. template <typename R, typename... ITs>
  926. struct dispatcher<false, R, ITs...> {
  927. template <std::size_t B, typename F, typename... Vs>
  928. MPARK_ALWAYS_INLINE static constexpr R dispatch(
  929. F &&, typename ITs::type &&..., Vs &&...) {
  930. MPARK_BUILTIN_UNREACHABLE;
  931. }
  932. template <std::size_t I, typename F, typename... Vs>
  933. MPARK_ALWAYS_INLINE static constexpr R dispatch_case(F &&, Vs &&...) {
  934. MPARK_BUILTIN_UNREACHABLE;
  935. }
  936. template <std::size_t B, typename F, typename... Vs>
  937. MPARK_ALWAYS_INLINE static constexpr R dispatch_at(std::size_t,
  938. F &&,
  939. Vs &&...) {
  940. MPARK_BUILTIN_UNREACHABLE;
  941. }
  942. };
  943. template <typename R, typename... ITs>
  944. struct dispatcher<true, R, ITs...> {
  945. template <std::size_t B, typename F>
  946. MPARK_ALWAYS_INLINE static constexpr R dispatch(
  947. F &&f, typename ITs::type &&... visited_vs) {
  948. using Expected = R;
  949. using Actual = decltype(lib::invoke(
  950. lib::forward<F>(f),
  951. access::base::get_alt<ITs::value>(
  952. lib::forward<typename ITs::type>(visited_vs))...));
  953. return visit_return_type_check<Expected, Actual>::invoke(
  954. lib::forward<F>(f),
  955. access::base::get_alt<ITs::value>(
  956. lib::forward<typename ITs::type>(visited_vs))...);
  957. }
  958. template <std::size_t B, typename F, typename V, typename... Vs>
  959. MPARK_ALWAYS_INLINE static constexpr R dispatch(
  960. F &&f, typename ITs::type &&... visited_vs, V &&v, Vs &&... vs) {
  961. #define MPARK_DISPATCH(I) \
  962. dispatcher<(I < lib::decay_t<V>::size()), \
  963. R, \
  964. ITs..., \
  965. lib::indexed_type<I, V>>:: \
  966. template dispatch<0>(lib::forward<F>(f), \
  967. lib::forward<typename ITs::type>(visited_vs)..., \
  968. lib::forward<V>(v), \
  969. lib::forward<Vs>(vs)...)
  970. #define MPARK_DEFAULT(I) \
  971. dispatcher<(I < lib::decay_t<V>::size()), R, ITs...>::template dispatch<I>( \
  972. lib::forward<F>(f), \
  973. lib::forward<typename ITs::type>(visited_vs)..., \
  974. lib::forward<V>(v), \
  975. lib::forward<Vs>(vs)...)
  976. switch (v.index()) {
  977. case B + 0: return MPARK_DISPATCH(B + 0);
  978. case B + 1: return MPARK_DISPATCH(B + 1);
  979. case B + 2: return MPARK_DISPATCH(B + 2);
  980. case B + 3: return MPARK_DISPATCH(B + 3);
  981. case B + 4: return MPARK_DISPATCH(B + 4);
  982. case B + 5: return MPARK_DISPATCH(B + 5);
  983. case B + 6: return MPARK_DISPATCH(B + 6);
  984. case B + 7: return MPARK_DISPATCH(B + 7);
  985. case B + 8: return MPARK_DISPATCH(B + 8);
  986. case B + 9: return MPARK_DISPATCH(B + 9);
  987. case B + 10: return MPARK_DISPATCH(B + 10);
  988. case B + 11: return MPARK_DISPATCH(B + 11);
  989. case B + 12: return MPARK_DISPATCH(B + 12);
  990. case B + 13: return MPARK_DISPATCH(B + 13);
  991. case B + 14: return MPARK_DISPATCH(B + 14);
  992. case B + 15: return MPARK_DISPATCH(B + 15);
  993. case B + 16: return MPARK_DISPATCH(B + 16);
  994. case B + 17: return MPARK_DISPATCH(B + 17);
  995. case B + 18: return MPARK_DISPATCH(B + 18);
  996. case B + 19: return MPARK_DISPATCH(B + 19);
  997. case B + 20: return MPARK_DISPATCH(B + 20);
  998. case B + 21: return MPARK_DISPATCH(B + 21);
  999. case B + 22: return MPARK_DISPATCH(B + 22);
  1000. case B + 23: return MPARK_DISPATCH(B + 23);
  1001. case B + 24: return MPARK_DISPATCH(B + 24);
  1002. case B + 25: return MPARK_DISPATCH(B + 25);
  1003. case B + 26: return MPARK_DISPATCH(B + 26);
  1004. case B + 27: return MPARK_DISPATCH(B + 27);
  1005. case B + 28: return MPARK_DISPATCH(B + 28);
  1006. case B + 29: return MPARK_DISPATCH(B + 29);
  1007. case B + 30: return MPARK_DISPATCH(B + 30);
  1008. case B + 31: return MPARK_DISPATCH(B + 31);
  1009. default: return MPARK_DEFAULT(B + 32);
  1010. }
  1011. #undef MPARK_DEFAULT
  1012. #undef MPARK_DISPATCH
  1013. }
  1014. template <std::size_t I, typename F, typename... Vs>
  1015. MPARK_ALWAYS_INLINE static constexpr R dispatch_case(F &&f,
  1016. Vs &&... vs) {
  1017. using Expected = R;
  1018. using Actual = decltype(
  1019. lib::invoke(lib::forward<F>(f),
  1020. access::base::get_alt<I>(lib::forward<Vs>(vs))...));
  1021. return visit_return_type_check<Expected, Actual>::invoke(
  1022. lib::forward<F>(f),
  1023. access::base::get_alt<I>(lib::forward<Vs>(vs))...);
  1024. }
  1025. template <std::size_t B, typename F, typename V, typename... Vs>
  1026. MPARK_ALWAYS_INLINE static constexpr R dispatch_at(std::size_t index,
  1027. F &&f,
  1028. V &&v,
  1029. Vs &&... vs) {
  1030. static_assert(lib::all<(lib::decay_t<V>::size() ==
  1031. lib::decay_t<Vs>::size())...>::value,
  1032. "all of the variants must be the same size.");
  1033. #define MPARK_DISPATCH_AT(I) \
  1034. dispatcher<(I < lib::decay_t<V>::size()), R>::template dispatch_case<I>( \
  1035. lib::forward<F>(f), lib::forward<V>(v), lib::forward<Vs>(vs)...)
  1036. #define MPARK_DEFAULT(I) \
  1037. dispatcher<(I < lib::decay_t<V>::size()), R>::template dispatch_at<I>( \
  1038. index, lib::forward<F>(f), lib::forward<V>(v), lib::forward<Vs>(vs)...)
  1039. switch (index) {
  1040. case B + 0: return MPARK_DISPATCH_AT(B + 0);
  1041. case B + 1: return MPARK_DISPATCH_AT(B + 1);
  1042. case B + 2: return MPARK_DISPATCH_AT(B + 2);
  1043. case B + 3: return MPARK_DISPATCH_AT(B + 3);
  1044. case B + 4: return MPARK_DISPATCH_AT(B + 4);
  1045. case B + 5: return MPARK_DISPATCH_AT(B + 5);
  1046. case B + 6: return MPARK_DISPATCH_AT(B + 6);
  1047. case B + 7: return MPARK_DISPATCH_AT(B + 7);
  1048. case B + 8: return MPARK_DISPATCH_AT(B + 8);
  1049. case B + 9: return MPARK_DISPATCH_AT(B + 9);
  1050. case B + 10: return MPARK_DISPATCH_AT(B + 10);
  1051. case B + 11: return MPARK_DISPATCH_AT(B + 11);
  1052. case B + 12: return MPARK_DISPATCH_AT(B + 12);
  1053. case B + 13: return MPARK_DISPATCH_AT(B + 13);
  1054. case B + 14: return MPARK_DISPATCH_AT(B + 14);
  1055. case B + 15: return MPARK_DISPATCH_AT(B + 15);
  1056. case B + 16: return MPARK_DISPATCH_AT(B + 16);
  1057. case B + 17: return MPARK_DISPATCH_AT(B + 17);
  1058. case B + 18: return MPARK_DISPATCH_AT(B + 18);
  1059. case B + 19: return MPARK_DISPATCH_AT(B + 19);
  1060. case B + 20: return MPARK_DISPATCH_AT(B + 20);
  1061. case B + 21: return MPARK_DISPATCH_AT(B + 21);
  1062. case B + 22: return MPARK_DISPATCH_AT(B + 22);
  1063. case B + 23: return MPARK_DISPATCH_AT(B + 23);
  1064. case B + 24: return MPARK_DISPATCH_AT(B + 24);
  1065. case B + 25: return MPARK_DISPATCH_AT(B + 25);
  1066. case B + 26: return MPARK_DISPATCH_AT(B + 26);
  1067. case B + 27: return MPARK_DISPATCH_AT(B + 27);
  1068. case B + 28: return MPARK_DISPATCH_AT(B + 28);
  1069. case B + 29: return MPARK_DISPATCH_AT(B + 29);
  1070. case B + 30: return MPARK_DISPATCH_AT(B + 30);
  1071. case B + 31: return MPARK_DISPATCH_AT(B + 31);
  1072. default: return MPARK_DEFAULT(B + 32);
  1073. }
  1074. #undef MPARK_DEFAULT
  1075. #undef MPARK_DISPATCH_AT
  1076. }
  1077. };
  1078. #else
  1079. template <typename T>
  1080. inline static constexpr const T &at(const T &elem) noexcept {
  1081. return elem;
  1082. }
  1083. template <typename T, std::size_t N, typename... Is>
  1084. inline static constexpr const lib::remove_all_extents_t<T> &at(
  1085. const lib::array<T, N> &elems, std::size_t i, Is... is) noexcept {
  1086. return at(elems[i], is...);
  1087. }
  1088. template <typename F, typename... Fs>
  1089. inline static constexpr lib::array<lib::decay_t<F>, sizeof...(Fs) + 1>
  1090. make_farray(F &&f, Fs &&... fs) {
  1091. return {{lib::forward<F>(f), lib::forward<Fs>(fs)...}};
  1092. }
  1093. template <typename F, typename... Vs>
  1094. struct make_fmatrix_impl {
  1095. template <std::size_t... Is>
  1096. inline static constexpr dispatch_result_t<F, Vs...> dispatch(
  1097. F &&f, Vs &&... vs) {
  1098. using Expected = dispatch_result_t<F, Vs...>;
  1099. using Actual = decltype(lib::invoke(
  1100. lib::forward<F>(f),
  1101. access::base::get_alt<Is>(lib::forward<Vs>(vs))...));
  1102. return visit_return_type_check<Expected, Actual>::invoke(
  1103. lib::forward<F>(f),
  1104. access::base::get_alt<Is>(lib::forward<Vs>(vs))...);
  1105. }
  1106. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  1107. template <std::size_t... Is>
  1108. inline static constexpr auto impl(lib::index_sequence<Is...>) {
  1109. return &dispatch<Is...>;
  1110. }
  1111. template <typename Is, std::size_t... Js, typename... Ls>
  1112. inline static constexpr auto impl(Is,
  1113. lib::index_sequence<Js...>,
  1114. Ls... ls) {
  1115. return make_farray(impl(lib::push_back_t<Is, Js>{}, ls...)...);
  1116. }
  1117. #else
  1118. template <typename...>
  1119. struct impl;
  1120. template <std::size_t... Is>
  1121. struct impl<lib::index_sequence<Is...>> {
  1122. inline constexpr AUTO operator()() const
  1123. AUTO_RETURN(&dispatch<Is...>)
  1124. };
  1125. template <typename Is, std::size_t... Js, typename... Ls>
  1126. struct impl<Is, lib::index_sequence<Js...>, Ls...> {
  1127. inline constexpr AUTO operator()() const
  1128. AUTO_RETURN(
  1129. make_farray(impl<lib::push_back_t<Is, Js>, Ls...>{}()...))
  1130. };
  1131. #endif
  1132. };
  1133. #ifdef MPARK_RETURN_TYPE_DEDUCTION
  1134. template <typename F, typename... Vs>
  1135. inline static constexpr auto make_fmatrix() {
  1136. return make_fmatrix_impl<F, Vs...>::impl(
  1137. lib::index_sequence<>{},
  1138. lib::make_index_sequence<lib::decay_t<Vs>::size()>{}...);
  1139. }
  1140. #else
  1141. template <typename F, typename... Vs>
  1142. inline static constexpr AUTO make_fmatrix()
  1143. AUTO_RETURN(
  1144. typename make_fmatrix_impl<F, Vs...>::template impl<
  1145. lib::index_sequence<>,
  1146. lib::make_index_sequence<lib::decay_t<Vs>::size()>...>{}())
  1147. #endif
  1148. template <typename F, typename... Vs>
  1149. struct make_fdiagonal_impl {
  1150. template <std::size_t I>
  1151. inline static constexpr dispatch_result_t<F, Vs...> dispatch(
  1152. F &&f, Vs &&... vs) {
  1153. using Expected = dispatch_result_t<F, Vs...>;
  1154. using Actual = decltype(
  1155. lib::invoke(lib::forward<F>(f),
  1156. access::base::get_alt<I>(lib::forward<Vs>(vs))...));
  1157. return visit_return_type_check<Expected, Actual>::invoke(
  1158. lib::forward<F>(f),
  1159. access::base::get_alt<I>(lib::forward<Vs>(vs))...);
  1160. }
  1161. template <std::size_t... Is>
  1162. inline static constexpr AUTO impl(lib::index_sequence<Is...>)
  1163. AUTO_RETURN(make_farray(&dispatch<Is>...))
  1164. };
  1165. template <typename F, typename V, typename... Vs>
  1166. inline static constexpr auto make_fdiagonal()
  1167. -> decltype(make_fdiagonal_impl<F, V, Vs...>::impl(
  1168. lib::make_index_sequence<lib::decay_t<V>::size()>{})) {
  1169. static_assert(lib::all<(lib::decay_t<V>::size() ==
  1170. lib::decay_t<Vs>::size())...>::value,
  1171. "all of the variants must be the same size.");
  1172. return make_fdiagonal_impl<F, V, Vs...>::impl(
  1173. lib::make_index_sequence<lib::decay_t<V>::size()>{});
  1174. }
  1175. #endif
  1176. };
  1177. #if !defined(MPARK_VARIANT_SWITCH_VISIT) && \
  1178. (!defined(_MSC_VER) || _MSC_VER >= 1910)
  1179. template <typename F, typename... Vs>
  1180. using fmatrix_t = decltype(base::make_fmatrix<F, Vs...>());
  1181. template <typename F, typename... Vs>
  1182. struct fmatrix {
  1183. static constexpr fmatrix_t<F, Vs...> value =
  1184. base::make_fmatrix<F, Vs...>();
  1185. };
  1186. template <typename F, typename... Vs>
  1187. constexpr fmatrix_t<F, Vs...> fmatrix<F, Vs...>::value;
  1188. template <typename F, typename... Vs>
  1189. using fdiagonal_t = decltype(base::make_fdiagonal<F, Vs...>());
  1190. template <typename F, typename... Vs>
  1191. struct fdiagonal {
  1192. static constexpr fdiagonal_t<F, Vs...> value =
  1193. base::make_fdiagonal<F, Vs...>();
  1194. };
  1195. template <typename F, typename... Vs>
  1196. constexpr fdiagonal_t<F, Vs...> fdiagonal<F, Vs...>::value;
  1197. #endif
  1198. struct alt {
  1199. template <typename Visitor, typename... Vs>
  1200. inline static constexpr DECLTYPE_AUTO visit_alt(Visitor &&visitor,
  1201. Vs &&... vs)
  1202. #ifdef MPARK_VARIANT_SWITCH_VISIT
  1203. DECLTYPE_AUTO_RETURN(
  1204. base::dispatcher<
  1205. true,
  1206. base::dispatch_result_t<Visitor,
  1207. decltype(as_base(
  1208. lib::forward<Vs>(vs)))...>>::
  1209. template dispatch<0>(lib::forward<Visitor>(visitor),
  1210. as_base(lib::forward<Vs>(vs))...))
  1211. #elif !defined(_MSC_VER) || _MSC_VER >= 1910
  1212. DECLTYPE_AUTO_RETURN(base::at(
  1213. fmatrix<Visitor &&,
  1214. decltype(as_base(lib::forward<Vs>(vs)))...>::value,
  1215. vs.index()...)(lib::forward<Visitor>(visitor),
  1216. as_base(lib::forward<Vs>(vs))...))
  1217. #else
  1218. DECLTYPE_AUTO_RETURN(base::at(
  1219. base::make_fmatrix<Visitor &&,
  1220. decltype(as_base(lib::forward<Vs>(vs)))...>(),
  1221. vs.index()...)(lib::forward<Visitor>(visitor),
  1222. as_base(lib::forward<Vs>(vs))...))
  1223. #endif
  1224. template <typename Visitor, typename... Vs>
  1225. inline static constexpr DECLTYPE_AUTO visit_alt_at(std::size_t index,
  1226. Visitor &&visitor,
  1227. Vs &&... vs)
  1228. #ifdef MPARK_VARIANT_SWITCH_VISIT
  1229. DECLTYPE_AUTO_RETURN(
  1230. base::dispatcher<
  1231. true,
  1232. base::dispatch_result_t<Visitor,
  1233. decltype(as_base(
  1234. lib::forward<Vs>(vs)))...>>::
  1235. template dispatch_at<0>(index,
  1236. lib::forward<Visitor>(visitor),
  1237. as_base(lib::forward<Vs>(vs))...))
  1238. #elif !defined(_MSC_VER) || _MSC_VER >= 1910
  1239. DECLTYPE_AUTO_RETURN(base::at(
  1240. fdiagonal<Visitor &&,
  1241. decltype(as_base(lib::forward<Vs>(vs)))...>::value,
  1242. index)(lib::forward<Visitor>(visitor),
  1243. as_base(lib::forward<Vs>(vs))...))
  1244. #else
  1245. DECLTYPE_AUTO_RETURN(base::at(
  1246. base::make_fdiagonal<Visitor &&,
  1247. decltype(as_base(lib::forward<Vs>(vs)))...>(),
  1248. index)(lib::forward<Visitor>(visitor),
  1249. as_base(lib::forward<Vs>(vs))...))
  1250. #endif
  1251. };
  1252. struct variant {
  1253. private:
  1254. template <typename Visitor>
  1255. struct visitor {
  1256. template <typename... Values>
  1257. inline static constexpr bool does_not_handle() {
  1258. return lib::is_invocable<Visitor, Values...>::value;
  1259. }
  1260. };
  1261. template <typename Visitor, typename... Values>
  1262. struct visit_exhaustiveness_check {
  1263. static_assert(visitor<Visitor>::template does_not_handle<Values...>(),
  1264. "`visit` requires the visitor to be exhaustive.");
  1265. inline static constexpr DECLTYPE_AUTO invoke(Visitor &&visitor,
  1266. Values &&... values)
  1267. DECLTYPE_AUTO_RETURN(lib::invoke(lib::forward<Visitor>(visitor),
  1268. lib::forward<Values>(values)...))
  1269. };
  1270. template <typename Visitor>
  1271. struct value_visitor {
  1272. Visitor &&visitor_;
  1273. template <typename... Alts>
  1274. inline constexpr DECLTYPE_AUTO operator()(Alts &&... alts) const
  1275. DECLTYPE_AUTO_RETURN(
  1276. visit_exhaustiveness_check<
  1277. Visitor,
  1278. decltype((lib::forward<Alts>(alts).value))...>::
  1279. invoke(lib::forward<Visitor>(visitor_),
  1280. lib::forward<Alts>(alts).value...))
  1281. };
  1282. template <typename Visitor>
  1283. inline static constexpr AUTO make_value_visitor(Visitor &&visitor)
  1284. AUTO_RETURN(value_visitor<Visitor>{lib::forward<Visitor>(visitor)})
  1285. public:
  1286. template <typename Visitor, typename... Vs>
  1287. inline static constexpr DECLTYPE_AUTO visit_alt(Visitor &&visitor,
  1288. Vs &&... vs)
  1289. DECLTYPE_AUTO_RETURN(alt::visit_alt(lib::forward<Visitor>(visitor),
  1290. lib::forward<Vs>(vs).impl_...))
  1291. template <typename Visitor, typename... Vs>
  1292. inline static constexpr DECLTYPE_AUTO visit_alt_at(std::size_t index,
  1293. Visitor &&visitor,
  1294. Vs &&... vs)
  1295. DECLTYPE_AUTO_RETURN(
  1296. alt::visit_alt_at(index,
  1297. lib::forward<Visitor>(visitor),
  1298. lib::forward<Vs>(vs).impl_...))
  1299. template <typename Visitor, typename... Vs>
  1300. inline static constexpr DECLTYPE_AUTO visit_value(Visitor &&visitor,
  1301. Vs &&... vs)
  1302. DECLTYPE_AUTO_RETURN(
  1303. visit_alt(make_value_visitor(lib::forward<Visitor>(visitor)),
  1304. lib::forward<Vs>(vs)...))
  1305. template <typename Visitor, typename... Vs>
  1306. inline static constexpr DECLTYPE_AUTO visit_value_at(std::size_t index,
  1307. Visitor &&visitor,
  1308. Vs &&... vs)
  1309. DECLTYPE_AUTO_RETURN(
  1310. visit_alt_at(index,
  1311. make_value_visitor(lib::forward<Visitor>(visitor)),
  1312. lib::forward<Vs>(vs)...))
  1313. };
  1314. } // namespace visitation
  1315. template <std::size_t Index, typename T>
  1316. struct alt {
  1317. using value_type = T;
  1318. #ifdef _MSC_VER
  1319. #pragma warning(push)
  1320. #pragma warning(disable : 4244)
  1321. #endif
  1322. template <typename... Args>
  1323. inline explicit constexpr alt(in_place_t, Args &&... args)
  1324. : value(lib::forward<Args>(args)...) {}
  1325. #ifdef _MSC_VER
  1326. #pragma warning(pop)
  1327. #endif
  1328. T value;
  1329. };
  1330. template <Trait DestructibleTrait, std::size_t Index, typename... Ts>
  1331. union recursive_union;
  1332. template <Trait DestructibleTrait, std::size_t Index>
  1333. union recursive_union<DestructibleTrait, Index> {};
  1334. #define MPARK_VARIANT_RECURSIVE_UNION(destructible_trait, destructor) \
  1335. template <std::size_t Index, typename T, typename... Ts> \
  1336. union recursive_union<destructible_trait, Index, T, Ts...> { \
  1337. public: \
  1338. inline explicit constexpr recursive_union(valueless_t) noexcept \
  1339. : dummy_{} {} \
  1340. \
  1341. template <typename... Args> \
  1342. inline explicit constexpr recursive_union(in_place_index_t<0>, \
  1343. Args &&... args) \
  1344. : head_(in_place_t{}, lib::forward<Args>(args)...) {} \
  1345. \
  1346. template <std::size_t I, typename... Args> \
  1347. inline explicit constexpr recursive_union(in_place_index_t<I>, \
  1348. Args &&... args) \
  1349. : tail_(in_place_index_t<I - 1>{}, lib::forward<Args>(args)...) {} \
  1350. \
  1351. recursive_union(const recursive_union &) = default; \
  1352. recursive_union(recursive_union &&) = default; \
  1353. \
  1354. destructor \
  1355. \
  1356. recursive_union &operator=(const recursive_union &) = default; \
  1357. recursive_union &operator=(recursive_union &&) = default; \
  1358. \
  1359. private: \
  1360. char dummy_; \
  1361. alt<Index, T> head_; \
  1362. recursive_union<destructible_trait, Index + 1, Ts...> tail_; \
  1363. \
  1364. friend struct access::recursive_union; \
  1365. }
  1366. MPARK_VARIANT_RECURSIVE_UNION(Trait::TriviallyAvailable,
  1367. ~recursive_union() = default;);
  1368. MPARK_VARIANT_RECURSIVE_UNION(Trait::Available,
  1369. ~recursive_union() {});
  1370. MPARK_VARIANT_RECURSIVE_UNION(Trait::Unavailable,
  1371. ~recursive_union() = delete;);
  1372. #undef MPARK_VARIANT_RECURSIVE_UNION
  1373. using index_t = unsigned int;
  1374. template <Trait DestructibleTrait, typename... Ts>
  1375. class base {
  1376. public:
  1377. inline explicit constexpr base(valueless_t tag) noexcept
  1378. : data_(tag), index_(static_cast<index_t>(-1)) {}
  1379. template <std::size_t I, typename... Args>
  1380. inline explicit constexpr base(in_place_index_t<I>, Args &&... args)
  1381. : data_(in_place_index_t<I>{}, lib::forward<Args>(args)...),
  1382. index_(I) {}
  1383. inline constexpr bool valueless_by_exception() const noexcept {
  1384. return index_ == static_cast<index_t>(-1);
  1385. }
  1386. inline constexpr std::size_t index() const noexcept {
  1387. return valueless_by_exception() ? variant_npos : index_;
  1388. }
  1389. protected:
  1390. using data_t = recursive_union<DestructibleTrait, 0, Ts...>;
  1391. friend inline constexpr base &as_base(base &b) { return b; }
  1392. friend inline constexpr const base &as_base(const base &b) { return b; }
  1393. friend inline constexpr base &&as_base(base &&b) { return lib::move(b); }
  1394. friend inline constexpr const base &&as_base(const base &&b) { return lib::move(b); }
  1395. friend inline constexpr data_t &data(base &b) { return b.data_; }
  1396. friend inline constexpr const data_t &data(const base &b) { return b.data_; }
  1397. friend inline constexpr data_t &&data(base &&b) { return lib::move(b).data_; }
  1398. friend inline constexpr const data_t &&data(const base &&b) { return lib::move(b).data_; }
  1399. inline static constexpr std::size_t size() { return sizeof...(Ts); }
  1400. data_t data_;
  1401. index_t index_;
  1402. friend struct access::base;
  1403. friend struct visitation::base;
  1404. };
  1405. struct dtor {
  1406. #ifdef _MSC_VER
  1407. #pragma warning(push)
  1408. #pragma warning(disable : 4100)
  1409. #endif
  1410. template <typename Alt>
  1411. inline void operator()(Alt &alt) const noexcept { alt.~Alt(); }
  1412. #ifdef _MSC_VER
  1413. #pragma warning(pop)
  1414. #endif
  1415. };
  1416. #if !defined(_MSC_VER) || _MSC_VER >= 1910
  1417. #define MPARK_INHERITING_CTOR(type, base) using base::base;
  1418. #else
  1419. #define MPARK_INHERITING_CTOR(type, base) \
  1420. template <typename... Args> \
  1421. inline explicit constexpr type(Args &&... args) \
  1422. : base(lib::forward<Args>(args)...) {}
  1423. #endif
  1424. template <typename Traits, Trait = Traits::destructible_trait>
  1425. class destructor;
  1426. #define MPARK_VARIANT_DESTRUCTOR(destructible_trait, definition, destroy) \
  1427. template <typename... Ts> \
  1428. class destructor<traits<Ts...>, destructible_trait> \
  1429. : public base<destructible_trait, Ts...> { \
  1430. using super = base<destructible_trait, Ts...>; \
  1431. \
  1432. public: \
  1433. MPARK_INHERITING_CTOR(destructor, super) \
  1434. using super::operator=; \
  1435. \
  1436. destructor(const destructor &) = default; \
  1437. destructor(destructor &&) = default; \
  1438. definition \
  1439. destructor &operator=(const destructor &) = default; \
  1440. destructor &operator=(destructor &&) = default; \
  1441. \
  1442. protected: \
  1443. destroy \
  1444. }
  1445. MPARK_VARIANT_DESTRUCTOR(
  1446. Trait::TriviallyAvailable,
  1447. ~destructor() = default;,
  1448. inline void destroy() noexcept {
  1449. this->index_ = static_cast<index_t>(-1);
  1450. });
  1451. MPARK_VARIANT_DESTRUCTOR(
  1452. Trait::Available,
  1453. ~destructor() { destroy(); },
  1454. inline void destroy() noexcept {
  1455. if (!this->valueless_by_exception()) {
  1456. visitation::alt::visit_alt(dtor{}, *this);
  1457. }
  1458. this->index_ = static_cast<index_t>(-1);
  1459. });
  1460. MPARK_VARIANT_DESTRUCTOR(
  1461. Trait::Unavailable,
  1462. ~destructor() = delete;,
  1463. inline void destroy() noexcept = delete;);
  1464. #undef MPARK_VARIANT_DESTRUCTOR
  1465. template <typename Traits>
  1466. class constructor : public destructor<Traits> {
  1467. using super = destructor<Traits>;
  1468. public:
  1469. MPARK_INHERITING_CTOR(constructor, super)
  1470. using super::operator=;
  1471. protected:
  1472. #ifndef MPARK_GENERIC_LAMBDAS
  1473. struct ctor {
  1474. template <typename LhsAlt, typename RhsAlt>
  1475. inline void operator()(LhsAlt &lhs_alt, RhsAlt &&rhs_alt) const {
  1476. constructor::construct_alt(lhs_alt,
  1477. lib::forward<RhsAlt>(rhs_alt).value);
  1478. }
  1479. };
  1480. #endif
  1481. template <std::size_t I, typename T, typename... Args>
  1482. inline static T &construct_alt(alt<I, T> &a, Args &&... args) {
  1483. auto *result = ::new (static_cast<void *>(lib::addressof(a)))
  1484. alt<I, T>(in_place_t{}, lib::forward<Args>(args)...);
  1485. return result->value;
  1486. }
  1487. template <typename Rhs>
  1488. inline static void generic_construct(constructor &lhs, Rhs &&rhs) {
  1489. lhs.destroy();
  1490. if (!rhs.valueless_by_exception()) {
  1491. visitation::alt::visit_alt_at(
  1492. rhs.index(),
  1493. #ifdef MPARK_GENERIC_LAMBDAS
  1494. [](auto &lhs_alt, auto &&rhs_alt) {
  1495. constructor::construct_alt(
  1496. lhs_alt, lib::forward<decltype(rhs_alt)>(rhs_alt).value);
  1497. }
  1498. #else
  1499. ctor{}
  1500. #endif
  1501. ,
  1502. lhs,
  1503. lib::forward<Rhs>(rhs));
  1504. lhs.index_ = rhs.index_;
  1505. }
  1506. }
  1507. };
  1508. template <typename Traits, Trait = Traits::move_constructible_trait>
  1509. class move_constructor;
  1510. #define MPARK_VARIANT_MOVE_CONSTRUCTOR(move_constructible_trait, definition) \
  1511. template <typename... Ts> \
  1512. class move_constructor<traits<Ts...>, move_constructible_trait> \
  1513. : public constructor<traits<Ts...>> { \
  1514. using super = constructor<traits<Ts...>>; \
  1515. \
  1516. public: \
  1517. MPARK_INHERITING_CTOR(move_constructor, super) \
  1518. using super::operator=; \
  1519. \
  1520. move_constructor(const move_constructor &) = default; \
  1521. definition \
  1522. ~move_constructor() = default; \
  1523. move_constructor &operator=(const move_constructor &) = default; \
  1524. move_constructor &operator=(move_constructor &&) = default; \
  1525. }
  1526. MPARK_VARIANT_MOVE_CONSTRUCTOR(
  1527. Trait::TriviallyAvailable,
  1528. move_constructor(move_constructor &&that) = default;);
  1529. MPARK_VARIANT_MOVE_CONSTRUCTOR(
  1530. Trait::Available,
  1531. move_constructor(move_constructor &&that) noexcept(
  1532. lib::all<std::is_nothrow_move_constructible<Ts>::value...>::value)
  1533. : move_constructor(valueless_t{}) {
  1534. this->generic_construct(*this, lib::move(that));
  1535. });
  1536. MPARK_VARIANT_MOVE_CONSTRUCTOR(
  1537. Trait::Unavailable,
  1538. move_constructor(move_constructor &&) = delete;);
  1539. #undef MPARK_VARIANT_MOVE_CONSTRUCTOR
  1540. template <typename Traits, Trait = Traits::copy_constructible_trait>
  1541. class copy_constructor;
  1542. #define MPARK_VARIANT_COPY_CONSTRUCTOR(copy_constructible_trait, definition) \
  1543. template <typename... Ts> \
  1544. class copy_constructor<traits<Ts...>, copy_constructible_trait> \
  1545. : public move_constructor<traits<Ts...>> { \
  1546. using super = move_constructor<traits<Ts...>>; \
  1547. \
  1548. public: \
  1549. MPARK_INHERITING_CTOR(copy_constructor, super) \
  1550. using super::operator=; \
  1551. \
  1552. definition \
  1553. copy_constructor(copy_constructor &&) = default; \
  1554. ~copy_constructor() = default; \
  1555. copy_constructor &operator=(const copy_constructor &) = default; \
  1556. copy_constructor &operator=(copy_constructor &&) = default; \
  1557. }
  1558. MPARK_VARIANT_COPY_CONSTRUCTOR(
  1559. Trait::TriviallyAvailable,
  1560. copy_constructor(const copy_constructor &that) = default;);
  1561. MPARK_VARIANT_COPY_CONSTRUCTOR(
  1562. Trait::Available,
  1563. copy_constructor(const copy_constructor &that)
  1564. : copy_constructor(valueless_t{}) {
  1565. this->generic_construct(*this, that);
  1566. });
  1567. MPARK_VARIANT_COPY_CONSTRUCTOR(
  1568. Trait::Unavailable,
  1569. copy_constructor(const copy_constructor &) = delete;);
  1570. #undef MPARK_VARIANT_COPY_CONSTRUCTOR
  1571. template <typename Traits>
  1572. class assignment : public copy_constructor<Traits> {
  1573. using super = copy_constructor<Traits>;
  1574. public:
  1575. MPARK_INHERITING_CTOR(assignment, super)
  1576. using super::operator=;
  1577. template <std::size_t I, typename... Args>
  1578. inline /* auto & */ auto emplace(Args &&... args)
  1579. -> decltype(this->construct_alt(access::base::get_alt<I>(*this),
  1580. lib::forward<Args>(args)...)) {
  1581. this->destroy();
  1582. auto &result = this->construct_alt(access::base::get_alt<I>(*this),
  1583. lib::forward<Args>(args)...);
  1584. this->index_ = I;
  1585. return result;
  1586. }
  1587. protected:
  1588. #ifndef MPARK_GENERIC_LAMBDAS
  1589. template <typename That>
  1590. struct assigner {
  1591. template <typename ThisAlt, typename ThatAlt>
  1592. inline void operator()(ThisAlt &this_alt, ThatAlt &&that_alt) const {
  1593. self->assign_alt(this_alt, lib::forward<ThatAlt>(that_alt).value);
  1594. }
  1595. assignment *self;
  1596. };
  1597. #endif
  1598. template <std::size_t I, typename T, typename Arg>
  1599. inline void assign_alt(alt<I, T> &a, Arg &&arg) {
  1600. if (this->index() == I) {
  1601. #ifdef _MSC_VER
  1602. #pragma warning(push)
  1603. #pragma warning(disable : 4244)
  1604. #endif
  1605. a.value = lib::forward<Arg>(arg);
  1606. #ifdef _MSC_VER
  1607. #pragma warning(pop)
  1608. #endif
  1609. } else {
  1610. struct {
  1611. void operator()(std::true_type) const {
  1612. this_->emplace<I>(lib::forward<Arg>(arg_));
  1613. }
  1614. void operator()(std::false_type) const {
  1615. this_->emplace<I>(T(lib::forward<Arg>(arg_)));
  1616. }
  1617. assignment *this_;
  1618. Arg &&arg_;
  1619. } impl{this, lib::forward<Arg>(arg)};
  1620. impl(lib::bool_constant<
  1621. std::is_nothrow_constructible<T, Arg>::value ||
  1622. !std::is_nothrow_move_constructible<T>::value>{});
  1623. }
  1624. }
  1625. template <typename That>
  1626. inline void generic_assign(That &&that) {
  1627. if (this->valueless_by_exception() && that.valueless_by_exception()) {
  1628. // do nothing.
  1629. } else if (that.valueless_by_exception()) {
  1630. this->destroy();
  1631. } else {
  1632. visitation::alt::visit_alt_at(
  1633. that.index(),
  1634. #ifdef MPARK_GENERIC_LAMBDAS
  1635. [this](auto &this_alt, auto &&that_alt) {
  1636. this->assign_alt(
  1637. this_alt, lib::forward<decltype(that_alt)>(that_alt).value);
  1638. }
  1639. #else
  1640. assigner<That>{this}
  1641. #endif
  1642. ,
  1643. *this,
  1644. lib::forward<That>(that));
  1645. }
  1646. }
  1647. };
  1648. template <typename Traits, Trait = Traits::move_assignable_trait>
  1649. class move_assignment;
  1650. #define MPARK_VARIANT_MOVE_ASSIGNMENT(move_assignable_trait, definition) \
  1651. template <typename... Ts> \
  1652. class move_assignment<traits<Ts...>, move_assignable_trait> \
  1653. : public assignment<traits<Ts...>> { \
  1654. using super = assignment<traits<Ts...>>; \
  1655. \
  1656. public: \
  1657. MPARK_INHERITING_CTOR(move_assignment, super) \
  1658. using super::operator=; \
  1659. \
  1660. move_assignment(const move_assignment &) = default; \
  1661. move_assignment(move_assignment &&) = default; \
  1662. ~move_assignment() = default; \
  1663. move_assignment &operator=(const move_assignment &) = default; \
  1664. definition \
  1665. }
  1666. MPARK_VARIANT_MOVE_ASSIGNMENT(
  1667. Trait::TriviallyAvailable,
  1668. move_assignment &operator=(move_assignment &&that) = default;);
  1669. MPARK_VARIANT_MOVE_ASSIGNMENT(
  1670. Trait::Available,
  1671. move_assignment &
  1672. operator=(move_assignment &&that) noexcept(
  1673. lib::all<(std::is_nothrow_move_constructible<Ts>::value &&
  1674. std::is_nothrow_move_assignable<Ts>::value)...>::value) {
  1675. this->generic_assign(lib::move(that));
  1676. return *this;
  1677. });
  1678. MPARK_VARIANT_MOVE_ASSIGNMENT(
  1679. Trait::Unavailable,
  1680. move_assignment &operator=(move_assignment &&) = delete;);
  1681. #undef MPARK_VARIANT_MOVE_ASSIGNMENT
  1682. template <typename Traits, Trait = Traits::copy_assignable_trait>
  1683. class copy_assignment;
  1684. #define MPARK_VARIANT_COPY_ASSIGNMENT(copy_assignable_trait, definition) \
  1685. template <typename... Ts> \
  1686. class copy_assignment<traits<Ts...>, copy_assignable_trait> \
  1687. : public move_assignment<traits<Ts...>> { \
  1688. using super = move_assignment<traits<Ts...>>; \
  1689. \
  1690. public: \
  1691. MPARK_INHERITING_CTOR(copy_assignment, super) \
  1692. using super::operator=; \
  1693. \
  1694. copy_assignment(const copy_assignment &) = default; \
  1695. copy_assignment(copy_assignment &&) = default; \
  1696. ~copy_assignment() = default; \
  1697. definition \
  1698. copy_assignment &operator=(copy_assignment &&) = default; \
  1699. }
  1700. MPARK_VARIANT_COPY_ASSIGNMENT(
  1701. Trait::TriviallyAvailable,
  1702. copy_assignment &operator=(const copy_assignment &that) = default;);
  1703. MPARK_VARIANT_COPY_ASSIGNMENT(
  1704. Trait::Available,
  1705. copy_assignment &operator=(const copy_assignment &that) {
  1706. this->generic_assign(that);
  1707. return *this;
  1708. });
  1709. MPARK_VARIANT_COPY_ASSIGNMENT(
  1710. Trait::Unavailable,
  1711. copy_assignment &operator=(const copy_assignment &) = delete;);
  1712. #undef MPARK_VARIANT_COPY_ASSIGNMENT
  1713. template <typename... Ts>
  1714. class impl : public copy_assignment<traits<Ts...>> {
  1715. using super = copy_assignment<traits<Ts...>>;
  1716. public:
  1717. MPARK_INHERITING_CTOR(impl, super)
  1718. using super::operator=;
  1719. impl(const impl&) = default;
  1720. impl(impl&&) = default;
  1721. ~impl() = default;
  1722. impl &operator=(const impl &) = default;
  1723. impl &operator=(impl &&) = default;
  1724. template <std::size_t I, typename Arg>
  1725. inline void assign(Arg &&arg) {
  1726. this->assign_alt(access::base::get_alt<I>(*this),
  1727. lib::forward<Arg>(arg));
  1728. }
  1729. inline void swap(impl &that) {
  1730. if (this->valueless_by_exception() && that.valueless_by_exception()) {
  1731. // do nothing.
  1732. } else if (this->index() == that.index()) {
  1733. visitation::alt::visit_alt_at(this->index(),
  1734. #ifdef MPARK_GENERIC_LAMBDAS
  1735. [](auto &this_alt, auto &that_alt) {
  1736. using std::swap;
  1737. swap(this_alt.value,
  1738. that_alt.value);
  1739. }
  1740. #else
  1741. swapper{}
  1742. #endif
  1743. ,
  1744. *this,
  1745. that);
  1746. } else {
  1747. impl *lhs = this;
  1748. impl *rhs = lib::addressof(that);
  1749. if (lhs->move_nothrow() && !rhs->move_nothrow()) {
  1750. std::swap(lhs, rhs);
  1751. }
  1752. impl tmp(lib::move(*rhs));
  1753. #ifdef MPARK_EXCEPTIONS
  1754. // EXTENSION: When the move construction of `lhs` into `rhs` throws
  1755. // and `tmp` is nothrow move constructible then we move `tmp` back
  1756. // into `rhs` and provide the strong exception safety guarantee.
  1757. try {
  1758. this->generic_construct(*rhs, lib::move(*lhs));
  1759. } catch (...) {
  1760. if (tmp.move_nothrow()) {
  1761. this->generic_construct(*rhs, lib::move(tmp));
  1762. }
  1763. throw;
  1764. }
  1765. #else
  1766. this->generic_construct(*rhs, lib::move(*lhs));
  1767. #endif
  1768. this->generic_construct(*lhs, lib::move(tmp));
  1769. }
  1770. }
  1771. private:
  1772. #ifndef MPARK_GENERIC_LAMBDAS
  1773. struct swapper {
  1774. template <typename ThisAlt, typename ThatAlt>
  1775. inline void operator()(ThisAlt &this_alt, ThatAlt &that_alt) const {
  1776. using std::swap;
  1777. swap(this_alt.value, that_alt.value);
  1778. }
  1779. };
  1780. #endif
  1781. inline constexpr bool move_nothrow() const {
  1782. return this->valueless_by_exception() ||
  1783. lib::array<bool, sizeof...(Ts)>{
  1784. {std::is_nothrow_move_constructible<Ts>::value...}
  1785. }[this->index()];
  1786. }
  1787. };
  1788. #undef MPARK_INHERITING_CTOR
  1789. template <std::size_t I, typename T>
  1790. struct overload_leaf {
  1791. using F = lib::size_constant<I> (*)(T);
  1792. operator F() const { return nullptr; }
  1793. };
  1794. template <typename... Ts>
  1795. struct overload_impl {
  1796. private:
  1797. template <typename>
  1798. struct impl;
  1799. template <std::size_t... Is>
  1800. struct impl<lib::index_sequence<Is...>> : overload_leaf<Is, Ts>... {};
  1801. public:
  1802. using type = impl<lib::index_sequence_for<Ts...>>;
  1803. };
  1804. template <typename... Ts>
  1805. using overload = typename overload_impl<Ts...>::type;
  1806. template <typename T, typename... Ts>
  1807. using best_match = lib::invoke_result_t<overload<Ts...>, T &&>;
  1808. template <typename T>
  1809. struct is_in_place_index : std::false_type {};
  1810. template <std::size_t I>
  1811. struct is_in_place_index<in_place_index_t<I>> : std::true_type {};
  1812. template <typename T>
  1813. struct is_in_place_type : std::false_type {};
  1814. template <typename T>
  1815. struct is_in_place_type<in_place_type_t<T>> : std::true_type {};
  1816. } // detail
  1817. template <typename... Ts>
  1818. class variant {
  1819. static_assert(0 < sizeof...(Ts),
  1820. "variant must consist of at least one alternative.");
  1821. static_assert(lib::all<!std::is_array<Ts>::value...>::value,
  1822. "variant can not have an array type as an alternative.");
  1823. static_assert(lib::all<!std::is_reference<Ts>::value...>::value,
  1824. "variant can not have a reference type as an alternative.");
  1825. static_assert(lib::all<!std::is_void<Ts>::value...>::value,
  1826. "variant can not have a void type as an alternative.");
  1827. public:
  1828. template <
  1829. typename Front = lib::type_pack_element_t<0, Ts...>,
  1830. lib::enable_if_t<std::is_default_constructible<Front>::value, int> = 0>
  1831. inline constexpr variant() noexcept(
  1832. std::is_nothrow_default_constructible<Front>::value)
  1833. : impl_(in_place_index_t<0>{}) {}
  1834. variant(const variant &) = default;
  1835. variant(variant &&) = default;
  1836. template <
  1837. typename Arg,
  1838. typename Decayed = lib::decay_t<Arg>,
  1839. lib::enable_if_t<!std::is_same<Decayed, variant>::value, int> = 0,
  1840. lib::enable_if_t<!detail::is_in_place_index<Decayed>::value, int> = 0,
  1841. lib::enable_if_t<!detail::is_in_place_type<Decayed>::value, int> = 0,
  1842. std::size_t I = detail::best_match<Arg, Ts...>::value,
  1843. typename T = lib::type_pack_element_t<I, Ts...>,
  1844. lib::enable_if_t<std::is_constructible<T, Arg>::value, int> = 0>
  1845. inline constexpr variant(Arg &&arg) noexcept(
  1846. std::is_nothrow_constructible<T, Arg>::value)
  1847. : impl_(in_place_index_t<I>{}, lib::forward<Arg>(arg)) {}
  1848. template <
  1849. std::size_t I,
  1850. typename... Args,
  1851. typename T = lib::type_pack_element_t<I, Ts...>,
  1852. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1853. inline explicit constexpr variant(
  1854. in_place_index_t<I>,
  1855. Args &&... args) noexcept(std::is_nothrow_constructible<T,
  1856. Args...>::value)
  1857. : impl_(in_place_index_t<I>{}, lib::forward<Args>(args)...) {}
  1858. template <
  1859. std::size_t I,
  1860. typename Up,
  1861. typename... Args,
  1862. typename T = lib::type_pack_element_t<I, Ts...>,
  1863. lib::enable_if_t<std::is_constructible<T,
  1864. std::initializer_list<Up> &,
  1865. Args...>::value,
  1866. int> = 0>
  1867. inline explicit constexpr variant(
  1868. in_place_index_t<I>,
  1869. std::initializer_list<Up> il,
  1870. Args &&... args) noexcept(std::
  1871. is_nothrow_constructible<
  1872. T,
  1873. std::initializer_list<Up> &,
  1874. Args...>::value)
  1875. : impl_(in_place_index_t<I>{}, il, lib::forward<Args>(args)...) {}
  1876. template <
  1877. typename T,
  1878. typename... Args,
  1879. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1880. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1881. inline explicit constexpr variant(
  1882. in_place_type_t<T>,
  1883. Args &&... args) noexcept(std::is_nothrow_constructible<T,
  1884. Args...>::value)
  1885. : impl_(in_place_index_t<I>{}, lib::forward<Args>(args)...) {}
  1886. template <
  1887. typename T,
  1888. typename Up,
  1889. typename... Args,
  1890. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1891. lib::enable_if_t<std::is_constructible<T,
  1892. std::initializer_list<Up> &,
  1893. Args...>::value,
  1894. int> = 0>
  1895. inline explicit constexpr variant(
  1896. in_place_type_t<T>,
  1897. std::initializer_list<Up> il,
  1898. Args &&... args) noexcept(std::
  1899. is_nothrow_constructible<
  1900. T,
  1901. std::initializer_list<Up> &,
  1902. Args...>::value)
  1903. : impl_(in_place_index_t<I>{}, il, lib::forward<Args>(args)...) {}
  1904. ~variant() = default;
  1905. variant &operator=(const variant &) = default;
  1906. variant &operator=(variant &&) = default;
  1907. template <typename Arg,
  1908. lib::enable_if_t<!std::is_same<lib::decay_t<Arg>, variant>::value,
  1909. int> = 0,
  1910. std::size_t I = detail::best_match<Arg, Ts...>::value,
  1911. typename T = lib::type_pack_element_t<I, Ts...>,
  1912. lib::enable_if_t<(std::is_assignable<T &, Arg>::value &&
  1913. std::is_constructible<T, Arg>::value),
  1914. int> = 0>
  1915. inline variant &operator=(Arg &&arg) noexcept(
  1916. (std::is_nothrow_assignable<T &, Arg>::value &&
  1917. std::is_nothrow_constructible<T, Arg>::value)) {
  1918. impl_.template assign<I>(lib::forward<Arg>(arg));
  1919. return *this;
  1920. }
  1921. template <
  1922. std::size_t I,
  1923. typename... Args,
  1924. typename T = lib::type_pack_element_t<I, Ts...>,
  1925. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1926. inline T &emplace(Args &&... args) {
  1927. return impl_.template emplace<I>(lib::forward<Args>(args)...);
  1928. }
  1929. template <
  1930. std::size_t I,
  1931. typename Up,
  1932. typename... Args,
  1933. typename T = lib::type_pack_element_t<I, Ts...>,
  1934. lib::enable_if_t<std::is_constructible<T,
  1935. std::initializer_list<Up> &,
  1936. Args...>::value,
  1937. int> = 0>
  1938. inline T &emplace(std::initializer_list<Up> il, Args &&... args) {
  1939. return impl_.template emplace<I>(il, lib::forward<Args>(args)...);
  1940. }
  1941. template <
  1942. typename T,
  1943. typename... Args,
  1944. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1945. lib::enable_if_t<std::is_constructible<T, Args...>::value, int> = 0>
  1946. inline T &emplace(Args &&... args) {
  1947. return impl_.template emplace<I>(lib::forward<Args>(args)...);
  1948. }
  1949. template <
  1950. typename T,
  1951. typename Up,
  1952. typename... Args,
  1953. std::size_t I = detail::find_index_sfinae<T, Ts...>::value,
  1954. lib::enable_if_t<std::is_constructible<T,
  1955. std::initializer_list<Up> &,
  1956. Args...>::value,
  1957. int> = 0>
  1958. inline T &emplace(std::initializer_list<Up> il, Args &&... args) {
  1959. return impl_.template emplace<I>(il, lib::forward<Args>(args)...);
  1960. }
  1961. inline constexpr bool valueless_by_exception() const noexcept {
  1962. return impl_.valueless_by_exception();
  1963. }
  1964. inline constexpr std::size_t index() const noexcept {
  1965. return impl_.index();
  1966. }
  1967. template <bool Dummy = true,
  1968. lib::enable_if_t<
  1969. lib::all<Dummy,
  1970. (lib::dependent_type<std::is_move_constructible<Ts>,
  1971. Dummy>::value &&
  1972. lib::dependent_type<lib::is_swappable<Ts>,
  1973. Dummy>::value)...>::value,
  1974. int> = 0>
  1975. inline void swap(variant &that) noexcept(
  1976. lib::all<(std::is_nothrow_move_constructible<Ts>::value &&
  1977. lib::is_nothrow_swappable<Ts>::value)...>::value) {
  1978. impl_.swap(that.impl_);
  1979. }
  1980. private:
  1981. detail::impl<Ts...> impl_;
  1982. friend struct detail::access::variant;
  1983. friend struct detail::visitation::variant;
  1984. };
  1985. template <std::size_t I, typename... Ts>
  1986. inline constexpr bool holds_alternative(const variant<Ts...> &v) noexcept {
  1987. return v.index() == I;
  1988. }
  1989. template <typename T, typename... Ts>
  1990. inline constexpr bool holds_alternative(const variant<Ts...> &v) noexcept {
  1991. return holds_alternative<detail::find_index_checked<T, Ts...>::value>(v);
  1992. }
  1993. namespace detail {
  1994. template <std::size_t I, typename V>
  1995. struct generic_get_impl {
  1996. constexpr generic_get_impl(int) noexcept {}
  1997. constexpr AUTO_REFREF operator()(V &&v) const
  1998. AUTO_REFREF_RETURN(
  1999. access::variant::get_alt<I>(lib::forward<V>(v)).value)
  2000. };
  2001. template <std::size_t I, typename V>
  2002. inline constexpr AUTO_REFREF generic_get(V &&v)
  2003. AUTO_REFREF_RETURN(generic_get_impl<I, V>(
  2004. holds_alternative<I>(v) ? 0 : (throw_bad_variant_access(), 0))(
  2005. lib::forward<V>(v)))
  2006. } // namespace detail
  2007. template <std::size_t I, typename... Ts>
  2008. inline constexpr variant_alternative_t<I, variant<Ts...>> &get(
  2009. variant<Ts...> &v) {
  2010. return detail::generic_get<I>(v);
  2011. }
  2012. template <std::size_t I, typename... Ts>
  2013. inline constexpr variant_alternative_t<I, variant<Ts...>> &&get(
  2014. variant<Ts...> &&v) {
  2015. return detail::generic_get<I>(lib::move(v));
  2016. }
  2017. template <std::size_t I, typename... Ts>
  2018. inline constexpr const variant_alternative_t<I, variant<Ts...>> &get(
  2019. const variant<Ts...> &v) {
  2020. return detail::generic_get<I>(v);
  2021. }
  2022. template <std::size_t I, typename... Ts>
  2023. inline constexpr const variant_alternative_t<I, variant<Ts...>> &&get(
  2024. const variant<Ts...> &&v) {
  2025. return detail::generic_get<I>(lib::move(v));
  2026. }
  2027. template <typename T, typename... Ts>
  2028. inline constexpr T &get(variant<Ts...> &v) {
  2029. return get<detail::find_index_checked<T, Ts...>::value>(v);
  2030. }
  2031. template <typename T, typename... Ts>
  2032. inline constexpr T &&get(variant<Ts...> &&v) {
  2033. return get<detail::find_index_checked<T, Ts...>::value>(lib::move(v));
  2034. }
  2035. template <typename T, typename... Ts>
  2036. inline constexpr const T &get(const variant<Ts...> &v) {
  2037. return get<detail::find_index_checked<T, Ts...>::value>(v);
  2038. }
  2039. template <typename T, typename... Ts>
  2040. inline constexpr const T &&get(const variant<Ts...> &&v) {
  2041. return get<detail::find_index_checked<T, Ts...>::value>(lib::move(v));
  2042. }
  2043. namespace detail {
  2044. template <std::size_t I, typename V>
  2045. inline constexpr /* auto * */ AUTO generic_get_if(V *v) noexcept
  2046. AUTO_RETURN(v && holds_alternative<I>(*v)
  2047. ? lib::addressof(access::variant::get_alt<I>(*v).value)
  2048. : nullptr)
  2049. } // namespace detail
  2050. template <std::size_t I, typename... Ts>
  2051. inline constexpr lib::add_pointer_t<variant_alternative_t<I, variant<Ts...>>>
  2052. get_if(variant<Ts...> *v) noexcept {
  2053. return detail::generic_get_if<I>(v);
  2054. }
  2055. template <std::size_t I, typename... Ts>
  2056. inline constexpr lib::add_pointer_t<
  2057. const variant_alternative_t<I, variant<Ts...>>>
  2058. get_if(const variant<Ts...> *v) noexcept {
  2059. return detail::generic_get_if<I>(v);
  2060. }
  2061. template <typename T, typename... Ts>
  2062. inline constexpr lib::add_pointer_t<T>
  2063. get_if(variant<Ts...> *v) noexcept {
  2064. return get_if<detail::find_index_checked<T, Ts...>::value>(v);
  2065. }
  2066. template <typename T, typename... Ts>
  2067. inline constexpr lib::add_pointer_t<const T>
  2068. get_if(const variant<Ts...> *v) noexcept {
  2069. return get_if<detail::find_index_checked<T, Ts...>::value>(v);
  2070. }
  2071. namespace detail {
  2072. template <typename RelOp>
  2073. struct convert_to_bool {
  2074. template <typename Lhs, typename Rhs>
  2075. inline constexpr bool operator()(Lhs &&lhs, Rhs &&rhs) const {
  2076. static_assert(std::is_convertible<lib::invoke_result_t<RelOp, Lhs, Rhs>,
  2077. bool>::value,
  2078. "relational operators must return a type"
  2079. " implicitly convertible to bool");
  2080. return lib::invoke(
  2081. RelOp{}, lib::forward<Lhs>(lhs), lib::forward<Rhs>(rhs));
  2082. }
  2083. };
  2084. } // namespace detail
  2085. template <typename... Ts>
  2086. inline constexpr bool operator==(const variant<Ts...> &lhs,
  2087. const variant<Ts...> &rhs) {
  2088. using detail::visitation::variant;
  2089. using equal_to = detail::convert_to_bool<lib::equal_to>;
  2090. #ifdef MPARK_CPP14_CONSTEXPR
  2091. if (lhs.index() != rhs.index()) return false;
  2092. if (lhs.valueless_by_exception()) return true;
  2093. return variant::visit_value_at(lhs.index(), equal_to{}, lhs, rhs);
  2094. #else
  2095. return lhs.index() == rhs.index() &&
  2096. (lhs.valueless_by_exception() ||
  2097. variant::visit_value_at(lhs.index(), equal_to{}, lhs, rhs));
  2098. #endif
  2099. }
  2100. template <typename... Ts>
  2101. inline constexpr bool operator!=(const variant<Ts...> &lhs,
  2102. const variant<Ts...> &rhs) {
  2103. using detail::visitation::variant;
  2104. using not_equal_to = detail::convert_to_bool<lib::not_equal_to>;
  2105. #ifdef MPARK_CPP14_CONSTEXPR
  2106. if (lhs.index() != rhs.index()) return true;
  2107. if (lhs.valueless_by_exception()) return false;
  2108. return variant::visit_value_at(lhs.index(), not_equal_to{}, lhs, rhs);
  2109. #else
  2110. return lhs.index() != rhs.index() ||
  2111. (!lhs.valueless_by_exception() &&
  2112. variant::visit_value_at(lhs.index(), not_equal_to{}, lhs, rhs));
  2113. #endif
  2114. }
  2115. template <typename... Ts>
  2116. inline constexpr bool operator<(const variant<Ts...> &lhs,
  2117. const variant<Ts...> &rhs) {
  2118. using detail::visitation::variant;
  2119. using less = detail::convert_to_bool<lib::less>;
  2120. #ifdef MPARK_CPP14_CONSTEXPR
  2121. if (rhs.valueless_by_exception()) return false;
  2122. if (lhs.valueless_by_exception()) return true;
  2123. if (lhs.index() < rhs.index()) return true;
  2124. if (lhs.index() > rhs.index()) return false;
  2125. return variant::visit_value_at(lhs.index(), less{}, lhs, rhs);
  2126. #else
  2127. return !rhs.valueless_by_exception() &&
  2128. (lhs.valueless_by_exception() || lhs.index() < rhs.index() ||
  2129. (lhs.index() == rhs.index() &&
  2130. variant::visit_value_at(lhs.index(), less{}, lhs, rhs)));
  2131. #endif
  2132. }
  2133. template <typename... Ts>
  2134. inline constexpr bool operator>(const variant<Ts...> &lhs,
  2135. const variant<Ts...> &rhs) {
  2136. using detail::visitation::variant;
  2137. using greater = detail::convert_to_bool<lib::greater>;
  2138. #ifdef MPARK_CPP14_CONSTEXPR
  2139. if (lhs.valueless_by_exception()) return false;
  2140. if (rhs.valueless_by_exception()) return true;
  2141. if (lhs.index() > rhs.index()) return true;
  2142. if (lhs.index() < rhs.index()) return false;
  2143. return variant::visit_value_at(lhs.index(), greater{}, lhs, rhs);
  2144. #else
  2145. return !lhs.valueless_by_exception() &&
  2146. (rhs.valueless_by_exception() || lhs.index() > rhs.index() ||
  2147. (lhs.index() == rhs.index() &&
  2148. variant::visit_value_at(lhs.index(), greater{}, lhs, rhs)));
  2149. #endif
  2150. }
  2151. template <typename... Ts>
  2152. inline constexpr bool operator<=(const variant<Ts...> &lhs,
  2153. const variant<Ts...> &rhs) {
  2154. using detail::visitation::variant;
  2155. using less_equal = detail::convert_to_bool<lib::less_equal>;
  2156. #ifdef MPARK_CPP14_CONSTEXPR
  2157. if (lhs.valueless_by_exception()) return true;
  2158. if (rhs.valueless_by_exception()) return false;
  2159. if (lhs.index() < rhs.index()) return true;
  2160. if (lhs.index() > rhs.index()) return false;
  2161. return variant::visit_value_at(lhs.index(), less_equal{}, lhs, rhs);
  2162. #else
  2163. return lhs.valueless_by_exception() ||
  2164. (!rhs.valueless_by_exception() &&
  2165. (lhs.index() < rhs.index() ||
  2166. (lhs.index() == rhs.index() &&
  2167. variant::visit_value_at(lhs.index(), less_equal{}, lhs, rhs))));
  2168. #endif
  2169. }
  2170. template <typename... Ts>
  2171. inline constexpr bool operator>=(const variant<Ts...> &lhs,
  2172. const variant<Ts...> &rhs) {
  2173. using detail::visitation::variant;
  2174. using greater_equal = detail::convert_to_bool<lib::greater_equal>;
  2175. #ifdef MPARK_CPP14_CONSTEXPR
  2176. if (rhs.valueless_by_exception()) return true;
  2177. if (lhs.valueless_by_exception()) return false;
  2178. if (lhs.index() > rhs.index()) return true;
  2179. if (lhs.index() < rhs.index()) return false;
  2180. return variant::visit_value_at(lhs.index(), greater_equal{}, lhs, rhs);
  2181. #else
  2182. return rhs.valueless_by_exception() ||
  2183. (!lhs.valueless_by_exception() &&
  2184. (lhs.index() > rhs.index() ||
  2185. (lhs.index() == rhs.index() &&
  2186. variant::visit_value_at(
  2187. lhs.index(), greater_equal{}, lhs, rhs))));
  2188. #endif
  2189. }
  2190. struct monostate {};
  2191. inline constexpr bool operator<(monostate, monostate) noexcept {
  2192. return false;
  2193. }
  2194. inline constexpr bool operator>(monostate, monostate) noexcept {
  2195. return false;
  2196. }
  2197. inline constexpr bool operator<=(monostate, monostate) noexcept {
  2198. return true;
  2199. }
  2200. inline constexpr bool operator>=(monostate, monostate) noexcept {
  2201. return true;
  2202. }
  2203. inline constexpr bool operator==(monostate, monostate) noexcept {
  2204. return true;
  2205. }
  2206. inline constexpr bool operator!=(monostate, monostate) noexcept {
  2207. return false;
  2208. }
  2209. #ifdef MPARK_CPP14_CONSTEXPR
  2210. namespace detail {
  2211. inline constexpr bool all(std::initializer_list<bool> bs) {
  2212. for (bool b : bs) {
  2213. if (!b) {
  2214. return false;
  2215. }
  2216. }
  2217. return true;
  2218. }
  2219. } // namespace detail
  2220. template <typename Visitor, typename... Vs>
  2221. inline constexpr decltype(auto) visit(Visitor &&visitor, Vs &&... vs) {
  2222. return (detail::all({!vs.valueless_by_exception()...})
  2223. ? (void)0
  2224. : throw_bad_variant_access()),
  2225. detail::visitation::variant::visit_value(
  2226. lib::forward<Visitor>(visitor), lib::forward<Vs>(vs)...);
  2227. }
  2228. #else
  2229. namespace detail {
  2230. template <std::size_t N>
  2231. inline constexpr bool all_impl(const lib::array<bool, N> &bs,
  2232. std::size_t idx) {
  2233. return idx >= N || (bs[idx] && all_impl(bs, idx + 1));
  2234. }
  2235. template <std::size_t N>
  2236. inline constexpr bool all(const lib::array<bool, N> &bs) {
  2237. return all_impl(bs, 0);
  2238. }
  2239. } // namespace detail
  2240. template <typename Visitor, typename... Vs>
  2241. inline constexpr DECLTYPE_AUTO visit(Visitor &&visitor, Vs &&... vs)
  2242. DECLTYPE_AUTO_RETURN(
  2243. (detail::all(
  2244. lib::array<bool, sizeof...(Vs)>{{!vs.valueless_by_exception()...}})
  2245. ? (void)0
  2246. : throw_bad_variant_access()),
  2247. detail::visitation::variant::visit_value(lib::forward<Visitor>(visitor),
  2248. lib::forward<Vs>(vs)...))
  2249. #endif
  2250. template <typename... Ts>
  2251. inline auto swap(variant<Ts...> &lhs,
  2252. variant<Ts...> &rhs) noexcept(noexcept(lhs.swap(rhs)))
  2253. -> decltype(lhs.swap(rhs)) {
  2254. lhs.swap(rhs);
  2255. }
  2256. namespace detail {
  2257. template <typename T, typename...>
  2258. using enabled_type = T;
  2259. namespace hash {
  2260. template <typename H, typename K>
  2261. constexpr bool meets_requirements() noexcept {
  2262. return std::is_copy_constructible<H>::value &&
  2263. std::is_move_constructible<H>::value &&
  2264. lib::is_invocable_r<std::size_t, H, const K &>::value;
  2265. }
  2266. template <typename K>
  2267. constexpr bool is_enabled() noexcept {
  2268. using H = std::hash<K>;
  2269. return meets_requirements<H, K>() &&
  2270. std::is_default_constructible<H>::value &&
  2271. std::is_copy_assignable<H>::value &&
  2272. std::is_move_assignable<H>::value;
  2273. }
  2274. } // namespace hash
  2275. } // namespace detail
  2276. #undef AUTO
  2277. #undef AUTO_RETURN
  2278. #undef AUTO_REFREF
  2279. #undef AUTO_REFREF_RETURN
  2280. #undef DECLTYPE_AUTO
  2281. #undef DECLTYPE_AUTO_RETURN
  2282. } // namespace mpark
  2283. namespace std {
  2284. template <typename... Ts>
  2285. struct hash<mpark::detail::enabled_type<
  2286. mpark::variant<Ts...>,
  2287. mpark::lib::enable_if_t<mpark::lib::all<mpark::detail::hash::is_enabled<
  2288. mpark::lib::remove_const_t<Ts>>()...>::value>>> {
  2289. using argument_type = mpark::variant<Ts...>;
  2290. using result_type = std::size_t;
  2291. inline result_type operator()(const argument_type &v) const {
  2292. using mpark::detail::visitation::variant;
  2293. std::size_t result =
  2294. v.valueless_by_exception()
  2295. ? 299792458 // Random value chosen by the universe upon creation
  2296. : variant::visit_alt(
  2297. #ifdef MPARK_GENERIC_LAMBDAS
  2298. [](const auto &alt) {
  2299. using alt_type = mpark::lib::decay_t<decltype(alt)>;
  2300. using value_type = mpark::lib::remove_const_t<
  2301. typename alt_type::value_type>;
  2302. return hash<value_type>{}(alt.value);
  2303. }
  2304. #else
  2305. hasher{}
  2306. #endif
  2307. ,
  2308. v);
  2309. return hash_combine(result, hash<std::size_t>{}(v.index()));
  2310. }
  2311. private:
  2312. #ifndef MPARK_GENERIC_LAMBDAS
  2313. struct hasher {
  2314. template <typename Alt>
  2315. inline std::size_t operator()(const Alt &alt) const {
  2316. using alt_type = mpark::lib::decay_t<Alt>;
  2317. using value_type =
  2318. mpark::lib::remove_const_t<typename alt_type::value_type>;
  2319. return hash<value_type>{}(alt.value);
  2320. }
  2321. };
  2322. #endif
  2323. static std::size_t hash_combine(std::size_t lhs, std::size_t rhs) {
  2324. return lhs ^= rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2);
  2325. }
  2326. };
  2327. template <>
  2328. struct hash<mpark::monostate> {
  2329. using argument_type = mpark::monostate;
  2330. using result_type = std::size_t;
  2331. inline result_type operator()(const argument_type &) const noexcept {
  2332. return 66740831; // return a fundamentally attractive random value.
  2333. }
  2334. };
  2335. } // namespace std
  2336. #endif // MPARK_VARIANT_HPP