You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

meta.hpp 127KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940
  1. /// \file meta.hpp Tiny meta-programming library.
  2. //
  3. // Meta library
  4. //
  5. // Copyright Eric Niebler 2014-present
  6. //
  7. // Use, modification and distribution is subject to the
  8. // Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. // Project home: https://github.com/ericniebler/meta
  13. //
  14. #ifndef META_HPP
  15. #define META_HPP
  16. #include <cstddef>
  17. #include <initializer_list>
  18. #include <meta/meta_fwd.hpp>
  19. #include <type_traits>
  20. #include <utility>
  21. #ifdef __clang__
  22. #pragma GCC diagnostic push
  23. #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  24. #pragma GCC diagnostic ignored "-Wpragmas"
  25. #pragma GCC diagnostic ignored "-Wdocumentation-deprecated-sync"
  26. #pragma GCC diagnostic ignored "-Wmissing-variable-declarations"
  27. #endif
  28. /// \defgroup meta Meta
  29. ///
  30. /// A tiny metaprogramming library
  31. /// \defgroup trait Trait
  32. /// Trait invocation/composition.
  33. /// \ingroup meta
  34. /// \defgroup invocation Invocation
  35. /// Trait invocation
  36. /// \ingroup trait
  37. /// \defgroup composition Composition
  38. /// Trait composition
  39. /// \ingroup trait
  40. /// \defgroup logical Logical
  41. /// Logical operations
  42. /// \ingroup meta
  43. /// \defgroup algorithm Algorithms
  44. /// Algorithms.
  45. /// \ingroup meta
  46. /// \defgroup query Query/Search
  47. /// Query and search algorithms
  48. /// \ingroup algorithm
  49. /// \defgroup transformation Transformation
  50. /// Transformation algorithms
  51. /// \ingroup algorithm
  52. /// \defgroup runtime Runtime
  53. /// Runtime algorithms
  54. /// \ingroup algorithm
  55. /// \defgroup datatype Datatype
  56. /// Datatypes.
  57. /// \ingroup meta
  58. /// \defgroup list list_like
  59. /// \ingroup datatype
  60. /// \defgroup integral Integer sequence
  61. /// Equivalent to C++14's `std::integer_sequence`
  62. /// \ingroup datatype
  63. /// \defgroup extension Extension
  64. /// Extend meta with your own datatypes.
  65. /// \ingroup datatype
  66. /// \defgroup math Math
  67. /// Integral constant arithmetic.
  68. /// \ingroup meta
  69. /// \defgroup lazy_trait lazy
  70. /// \ingroup trait
  71. /// \defgroup lazy_invocation lazy
  72. /// \ingroup invocation
  73. /// \defgroup lazy_composition lazy
  74. /// \ingroup composition
  75. /// \defgroup lazy_logical lazy
  76. /// \ingroup logical
  77. /// \defgroup lazy_query lazy
  78. /// \ingroup query
  79. /// \defgroup lazy_transformation lazy
  80. /// \ingroup transformation
  81. /// \defgroup lazy_list lazy
  82. /// \ingroup list
  83. /// \defgroup lazy_datatype lazy
  84. /// \ingroup datatype
  85. /// \defgroup lazy_math lazy
  86. /// \ingroup math
  87. /// Tiny metaprogramming library
  88. namespace meta
  89. {
  90. namespace detail
  91. {
  92. /// Returns a \p T nullptr
  93. template <typename T>
  94. constexpr T *_nullptr_v()
  95. {
  96. return nullptr;
  97. }
  98. #if META_CXX_VARIABLE_TEMPLATES
  99. template <typename T>
  100. META_INLINE_VAR constexpr T *nullptr_v = nullptr;
  101. #endif
  102. } // namespace detail
  103. /// An empty type.
  104. /// \ingroup datatype
  105. struct nil_
  106. {
  107. };
  108. /// Type alias for \p T::type.
  109. /// \ingroup invocation
  110. template <META_TYPE_CONSTRAINT(trait) T>
  111. using _t = typename T::type;
  112. #if META_CXX_VARIABLE_TEMPLATES || defined(META_DOXYGEN_INVOKED)
  113. /// Variable alias for \c T::type::value
  114. /// \note Requires C++14 or greater.
  115. /// \ingroup invocation
  116. template <META_TYPE_CONSTRAINT(integral) T>
  117. constexpr typename T::type::value_type _v = T::type::value;
  118. #endif
  119. /// Lazy versions of meta actions
  120. namespace lazy
  121. {
  122. /// \sa `meta::_t`
  123. /// \ingroup lazy_invocation
  124. template <typename T>
  125. using _t = defer<_t, T>;
  126. } // namespace lazy
  127. /// An integral constant wrapper for \c std::size_t.
  128. /// \ingroup integral
  129. template <std::size_t N>
  130. using size_t = std::integral_constant<std::size_t, N>;
  131. /// An integral constant wrapper for \c bool.
  132. /// \ingroup integral
  133. template <bool B>
  134. using bool_ = std::integral_constant<bool, B>;
  135. /// An integral constant wrapper for \c int.
  136. /// \ingroup integral
  137. template <int I>
  138. using int_ = std::integral_constant<int, I>;
  139. /// An integral constant wrapper for \c char.
  140. /// \ingroup integral
  141. template <char Ch>
  142. using char_ = std::integral_constant<char, Ch>;
  143. ///////////////////////////////////////////////////////////////////////////////////////////
  144. // Math operations
  145. /// An integral constant wrapper around the result of incrementing the wrapped integer \c
  146. /// T::type::value.
  147. template <META_TYPE_CONSTRAINT(integral) T>
  148. using inc = std::integral_constant<decltype(T::type::value + 1), T::type::value + 1>;
  149. /// An integral constant wrapper around the result of decrementing the wrapped integer \c
  150. /// T::type::value.
  151. template <META_TYPE_CONSTRAINT(integral) T>
  152. using dec = std::integral_constant<decltype(T::type::value - 1), T::type::value - 1>;
  153. /// An integral constant wrapper around the result of adding the two wrapped integers
  154. /// \c T::type::value and \c U::type::value.
  155. /// \ingroup math
  156. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  157. using plus = std::integral_constant<decltype(T::type::value + U::type::value),
  158. T::type::value + U::type::value>;
  159. /// An integral constant wrapper around the result of subtracting the two wrapped integers
  160. /// \c T::type::value and \c U::type::value.
  161. /// \ingroup math
  162. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  163. using minus = std::integral_constant<decltype(T::type::value - U::type::value),
  164. T::type::value - U::type::value>;
  165. /// An integral constant wrapper around the result of multiplying the two wrapped integers
  166. /// \c T::type::value and \c U::type::value.
  167. /// \ingroup math
  168. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  169. using multiplies = std::integral_constant<decltype(T::type::value * U::type::value),
  170. T::type::value * U::type::value>;
  171. /// An integral constant wrapper around the result of dividing the two wrapped integers \c
  172. /// T::type::value and \c U::type::value.
  173. /// \ingroup math
  174. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  175. using divides = std::integral_constant<decltype(T::type::value / U::type::value),
  176. T::type::value / U::type::value>;
  177. /// An integral constant wrapper around the result of negating the wrapped integer
  178. /// \c T::type::value.
  179. /// \ingroup math
  180. template <META_TYPE_CONSTRAINT(integral) T>
  181. using negate = std::integral_constant<decltype(-T::type::value), -T::type::value>;
  182. /// An integral constant wrapper around the remainder of dividing the two wrapped integers
  183. /// \c T::type::value and \c U::type::value.
  184. /// \ingroup math
  185. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  186. using modulus = std::integral_constant<decltype(T::type::value % U::type::value),
  187. T::type::value % U::type::value>;
  188. /// A Boolean integral constant wrapper around the result of comparing \c T::type::value and
  189. /// \c U::type::value for equality.
  190. /// \ingroup math
  191. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  192. using equal_to = bool_<T::type::value == U::type::value>;
  193. /// A Boolean integral constant wrapper around the result of comparing \c T::type::value and
  194. /// \c U::type::value for inequality.
  195. /// \ingroup math
  196. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  197. using not_equal_to = bool_<T::type::value != U::type::value>;
  198. /// A Boolean integral constant wrapper around \c true if \c T::type::value is greater than
  199. /// \c U::type::value; \c false, otherwise.
  200. /// \ingroup math
  201. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  202. using greater = bool_<(T::type::value > U::type::value)>;
  203. /// A Boolean integral constant wrapper around \c true if \c T::type::value is less than \c
  204. /// U::type::value; \c false, otherwise.
  205. /// \ingroup math
  206. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  207. using less = bool_<(T::type::value < U::type::value)>;
  208. /// A Boolean integral constant wrapper around \c true if \c T::type::value is greater than
  209. /// or equal to \c U::type::value; \c false, otherwise.
  210. /// \ingroup math
  211. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  212. using greater_equal = bool_<(T::type::value >= U::type::value)>;
  213. /// A Boolean integral constant wrapper around \c true if \c T::type::value is less than or
  214. /// equal to \c U::type::value; \c false, otherwise.
  215. /// \ingroup math
  216. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  217. using less_equal = bool_<(T::type::value <= U::type::value)>;
  218. /// An integral constant wrapper around the result of bitwise-and'ing the two wrapped
  219. /// integers \c T::type::value and \c U::type::value.
  220. /// \ingroup math
  221. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  222. using bit_and = std::integral_constant<decltype(T::type::value & U::type::value),
  223. T::type::value & U::type::value>;
  224. /// An integral constant wrapper around the result of bitwise-or'ing the two wrapped
  225. /// integers \c T::type::value and \c U::type::value.
  226. /// \ingroup math
  227. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  228. using bit_or = std::integral_constant<decltype(T::type::value | U::type::value),
  229. T::type::value | U::type::value>;
  230. /// An integral constant wrapper around the result of bitwise-exclusive-or'ing the two
  231. /// wrapped integers \c T::type::value and \c U::type::value.
  232. /// \ingroup math
  233. template <META_TYPE_CONSTRAINT(integral) T, META_TYPE_CONSTRAINT(integral) U>
  234. using bit_xor = std::integral_constant<decltype(T::type::value ^ U::type::value),
  235. T::type::value ^ U::type::value>;
  236. /// An integral constant wrapper around the result of bitwise-complementing the wrapped
  237. /// integer \c T::type::value.
  238. /// \ingroup math
  239. template <META_TYPE_CONSTRAINT(integral) T>
  240. using bit_not = std::integral_constant<decltype(~T::type::value), ~T::type::value>;
  241. namespace lazy
  242. {
  243. /// \sa 'meta::int'
  244. /// \ingroup lazy_math
  245. template <typename T>
  246. using inc = defer<inc, T>;
  247. /// \sa 'meta::dec'
  248. /// \ingroup lazy_math
  249. template <typename T>
  250. using dec = defer<dec, T>;
  251. /// \sa 'meta::plus'
  252. /// \ingroup lazy_math
  253. template <typename T, typename U>
  254. using plus = defer<plus, T, U>;
  255. /// \sa 'meta::minus'
  256. /// \ingroup lazy_math
  257. template <typename T, typename U>
  258. using minus = defer<minus, T, U>;
  259. /// \sa 'meta::multiplies'
  260. /// \ingroup lazy_math
  261. template <typename T, typename U>
  262. using multiplies = defer<multiplies, T, U>;
  263. /// \sa 'meta::divides'
  264. /// \ingroup lazy_math
  265. template <typename T, typename U>
  266. using divides = defer<divides, T, U>;
  267. /// \sa 'meta::negate'
  268. /// \ingroup lazy_math
  269. template <typename T>
  270. using negate = defer<negate, T>;
  271. /// \sa 'meta::modulus'
  272. /// \ingroup lazy_math
  273. template <typename T, typename U>
  274. using modulus = defer<modulus, T, U>;
  275. /// \sa 'meta::equal_to'
  276. /// \ingroup lazy_math
  277. template <typename T, typename U>
  278. using equal_to = defer<equal_to, T, U>;
  279. /// \sa 'meta::not_equal_t'
  280. /// \ingroup lazy_math
  281. template <typename T, typename U>
  282. using not_equal_to = defer<not_equal_to, T, U>;
  283. /// \sa 'meta::greater'
  284. /// \ingroup lazy_math
  285. template <typename T, typename U>
  286. using greater = defer<greater, T, U>;
  287. /// \sa 'meta::less'
  288. /// \ingroup lazy_math
  289. template <typename T, typename U>
  290. using less = defer<less, T, U>;
  291. /// \sa 'meta::greater_equal'
  292. /// \ingroup lazy_math
  293. template <typename T, typename U>
  294. using greater_equal = defer<greater_equal, T, U>;
  295. /// \sa 'meta::less_equal'
  296. /// \ingroup lazy_math
  297. template <typename T, typename U>
  298. using less_equal = defer<less_equal, T, U>;
  299. /// \sa 'meta::bit_and'
  300. /// \ingroup lazy_math
  301. template <typename T, typename U>
  302. using bit_and = defer<bit_and, T, U>;
  303. /// \sa 'meta::bit_or'
  304. /// \ingroup lazy_math
  305. template <typename T, typename U>
  306. using bit_or = defer<bit_or, T, U>;
  307. /// \sa 'meta::bit_xor'
  308. /// \ingroup lazy_math
  309. template <typename T, typename U>
  310. using bit_xor = defer<bit_xor, T, U>;
  311. /// \sa 'meta::bit_not'
  312. /// \ingroup lazy_math
  313. template <typename T>
  314. using bit_not = defer<bit_not, T>;
  315. } // namespace lazy
  316. /// \cond
  317. namespace detail
  318. {
  319. enum class indices_strategy_
  320. {
  321. done,
  322. repeat,
  323. recurse
  324. };
  325. constexpr indices_strategy_ strategy_(std::size_t cur, std::size_t end)
  326. {
  327. return cur >= end ? indices_strategy_::done
  328. : cur * 2 <= end ? indices_strategy_::repeat
  329. : indices_strategy_::recurse;
  330. }
  331. template <typename T>
  332. constexpr std::size_t range_distance_(T begin, T end)
  333. {
  334. return begin <= end ? static_cast<std::size_t>(end - begin)
  335. : throw "The start of the integer_sequence must not be "
  336. "greater than the end";
  337. }
  338. template <std::size_t End, typename State, indices_strategy_ Status_>
  339. struct make_indices_
  340. {
  341. using type = State;
  342. };
  343. template <typename T, T, typename>
  344. struct coerce_indices_
  345. {
  346. };
  347. } // namespace detail
  348. /// \endcond
  349. ///////////////////////////////////////////////////////////////////////////////////////////
  350. // integer_sequence
  351. #if !META_CXX_INTEGER_SEQUENCE
  352. /// A container for a sequence of compile-time integer constants.
  353. /// \ingroup integral
  354. template <typename T, T... Is>
  355. struct integer_sequence
  356. {
  357. using value_type = T;
  358. /// \return `sizeof...(Is)`
  359. static constexpr std::size_t size() noexcept { return sizeof...(Is); }
  360. };
  361. #endif
  362. ///////////////////////////////////////////////////////////////////////////////////////////
  363. // index_sequence
  364. /// A container for a sequence of compile-time integer constants of type
  365. /// \c std::size_t
  366. /// \ingroup integral
  367. template <std::size_t... Is>
  368. using index_sequence = integer_sequence<std::size_t, Is...>;
  369. #if META_HAS_MAKE_INTEGER_SEQ && !defined(META_DOXYGEN_INVOKED)
  370. // Implement make_integer_sequence and make_index_sequence with the
  371. // __make_integer_seq builtin on compilers that provide it. (Redirect
  372. // through decltype to workaround suspected clang bug.)
  373. /// \cond
  374. namespace detail
  375. {
  376. template <typename T, T N>
  377. __make_integer_seq<integer_sequence, T, N> make_integer_sequence_();
  378. }
  379. /// \endcond
  380. template <typename T, T N>
  381. using make_integer_sequence = decltype(detail::make_integer_sequence_<T, N>());
  382. template <std::size_t N>
  383. using make_index_sequence = make_integer_sequence<std::size_t, N>;
  384. #else
  385. /// Generate \c index_sequence containing integer constants [0,1,2,...,N-1].
  386. /// \par Complexity
  387. /// \f$ O(log(N)) \f$.
  388. /// \ingroup integral
  389. template <std::size_t N>
  390. using make_index_sequence =
  391. _t<detail::make_indices_<N, index_sequence<0>, detail::strategy_(1, N)>>;
  392. /// Generate \c integer_sequence containing integer constants [0,1,2,...,N-1].
  393. /// \par Complexity
  394. /// \f$ O(log(N)) \f$.
  395. /// \ingroup integral
  396. template <typename T, T N>
  397. using make_integer_sequence =
  398. _t<detail::coerce_indices_<T, 0, make_index_sequence<static_cast<std::size_t>(N)>>>;
  399. #endif
  400. ///////////////////////////////////////////////////////////////////////////////////////////
  401. // integer_range
  402. /// Makes the integer sequence <tt>[From, To)</tt>.
  403. /// \par Complexity
  404. /// \f$ O(log(To - From)) \f$.
  405. /// \ingroup integral
  406. template <typename T, T From, T To>
  407. using integer_range =
  408. _t<detail::coerce_indices_<T, From,
  409. make_index_sequence<detail::range_distance_(From, To)>>>;
  410. /// \cond
  411. namespace detail
  412. {
  413. template <typename, typename>
  414. struct concat_indices_
  415. {
  416. };
  417. template <std::size_t... Is, std::size_t... Js>
  418. struct concat_indices_<index_sequence<Is...>, index_sequence<Js...>>
  419. {
  420. using type = index_sequence<Is..., (Js + sizeof...(Is))...>;
  421. };
  422. template <>
  423. struct make_indices_<0u, index_sequence<0>, indices_strategy_::done>
  424. {
  425. using type = index_sequence<>;
  426. };
  427. template <std::size_t End, std::size_t... Values>
  428. struct make_indices_<End, index_sequence<Values...>, indices_strategy_::repeat>
  429. : make_indices_<End, index_sequence<Values..., (Values + sizeof...(Values))...>,
  430. detail::strategy_(sizeof...(Values) * 2, End)>
  431. {
  432. };
  433. template <std::size_t End, std::size_t... Values>
  434. struct make_indices_<End, index_sequence<Values...>, indices_strategy_::recurse>
  435. : concat_indices_<index_sequence<Values...>,
  436. make_index_sequence<End - sizeof...(Values)>>
  437. {
  438. };
  439. template <typename T, T Offset, std::size_t... Values>
  440. struct coerce_indices_<T, Offset, index_sequence<Values...>>
  441. {
  442. using type =
  443. integer_sequence<T, static_cast<T>(static_cast<T>(Values) + Offset)...>;
  444. };
  445. } // namespace detail
  446. /// \endcond
  447. /// Evaluate the invocable \p Fn with the arguments \p Args.
  448. /// \ingroup invocation
  449. template <META_TYPE_CONSTRAINT(invocable) Fn, typename... Args>
  450. using invoke = typename Fn::template invoke<Args...>;
  451. /// Lazy versions of meta actions
  452. namespace lazy
  453. {
  454. /// \sa `meta::invoke`
  455. /// \ingroup lazy_invocation
  456. template <typename Fn, typename... Args>
  457. using invoke = defer<invoke, Fn, Args...>;
  458. } // namespace lazy
  459. /// A trait that always returns its argument \p T. It is also an invocable
  460. /// that always returns \p T.
  461. /// \ingroup trait
  462. /// \ingroup invocation
  463. template <typename T>
  464. struct id
  465. {
  466. #if defined(META_WORKAROUND_CWG_1558) && !defined(META_DOXYGEN_INVOKED)
  467. // Redirect through decltype for compilers that have not
  468. // yet implemented CWG 1558:
  469. static id impl(void *);
  470. template <typename... Ts>
  471. using invoke = _t<decltype(id::impl(static_cast<list<Ts...> *>(nullptr)))>;
  472. #else
  473. template <typename...>
  474. using invoke = T;
  475. #endif
  476. using type = T;
  477. };
  478. /// An alias for type \p T. Useful in non-deduced contexts.
  479. /// \ingroup trait
  480. template <typename T>
  481. using id_t = _t<id<T>>;
  482. namespace lazy
  483. {
  484. /// \sa `meta::id`
  485. /// \ingroup lazy_trait
  486. /// \ingroup lazy_invocation
  487. template <typename T>
  488. using id = defer<id, T>;
  489. } // namespace lazy
  490. /// An alias for `void`.
  491. /// \ingroup trait
  492. #if defined(META_WORKAROUND_CWG_1558) && !defined(META_DOXYGEN_INVOKED)
  493. // Redirect through decltype for compilers that have not
  494. // yet implemented CWG 1558:
  495. template <typename... Ts>
  496. using void_ = invoke<id<void>, Ts...>;
  497. #else
  498. template <typename...>
  499. using void_ = void;
  500. #endif
  501. #if META_CXX_VARIABLE_TEMPLATES
  502. #ifdef META_CONCEPT
  503. /// `true` if `T::type` exists and names a type; `false` otherwise.
  504. /// \ingroup trait
  505. template <typename T>
  506. META_INLINE_VAR constexpr bool is_trait_v = trait<T>;
  507. /// `true` if `T::invoke` exists and names a class template; `false` otherwise.
  508. /// \ingroup trait
  509. template <typename T>
  510. META_INLINE_VAR constexpr bool is_callable_v = invocable<T>;
  511. #else // ^^^ Concepts / No concepts vvv
  512. /// \cond
  513. namespace detail
  514. {
  515. template <typename, typename = void>
  516. META_INLINE_VAR constexpr bool is_trait_ = false;
  517. template <typename T>
  518. META_INLINE_VAR constexpr bool is_trait_<T, void_<typename T::type>> = true;
  519. template <typename, typename = void>
  520. META_INLINE_VAR constexpr bool is_callable_ = false;
  521. template <typename T>
  522. META_INLINE_VAR constexpr bool is_callable_<T, void_<quote<T::template invoke>>> = true;
  523. } // namespace detail
  524. /// \endcond
  525. /// `true` if `T::type` exists and names a type; `false` otherwise.
  526. /// \ingroup trait
  527. template <typename T>
  528. META_INLINE_VAR constexpr bool is_trait_v = detail::is_trait_<T>;
  529. /// `true` if `T::invoke` exists and names a class template; `false` otherwise.
  530. /// \ingroup trait
  531. template <typename T>
  532. META_INLINE_VAR constexpr bool is_callable_v = detail::is_callable_<T>;
  533. #endif // Concepts vs. variable templates
  534. /// An alias for `std::true_type` if `T::type` exists and names a type; otherwise, it's an
  535. /// alias for `std::false_type`.
  536. /// \ingroup trait
  537. template <typename T>
  538. using is_trait = bool_<is_trait_v<T>>;
  539. /// An alias for `std::true_type` if `T::invoke` exists and names a class template;
  540. /// otherwise, it's an alias for `std::false_type`.
  541. /// \ingroup trait
  542. template <typename T>
  543. using is_callable = bool_<is_callable_v<T>>;
  544. #else // ^^^ META_CXX_VARIABLE_TEMPLATES / !META_CXX_VARIABLE_TEMPLATES vvv
  545. /// \cond
  546. namespace detail
  547. {
  548. template <typename, typename = void>
  549. struct is_trait_
  550. {
  551. using type = std::false_type;
  552. };
  553. template <typename T>
  554. struct is_trait_<T, void_<typename T::type>>
  555. {
  556. using type = std::true_type;
  557. };
  558. template <typename, typename = void>
  559. struct is_callable_
  560. {
  561. using type = std::false_type;
  562. };
  563. template <typename T>
  564. struct is_callable_<T, void_<quote<T::template invoke>>>
  565. {
  566. using type = std::true_type;
  567. };
  568. } // namespace detail
  569. /// \endcond
  570. template <typename T>
  571. using is_trait = _t<detail::is_trait_<T>>;
  572. /// An alias for `std::true_type` if `T::invoke` exists and names a class
  573. /// template or alias template; otherwise, it's an alias for
  574. /// `std::false_type`.
  575. /// \ingroup trait
  576. template <typename T>
  577. using is_callable = _t<detail::is_callable_<T>>;
  578. #endif
  579. /// \cond
  580. namespace detail
  581. {
  582. #ifdef META_CONCEPT
  583. template <template <typename...> class, typename...>
  584. struct defer_
  585. {
  586. };
  587. template <template <typename...> class C, typename... Ts>
  588. requires valid<C, Ts...> struct defer_<C, Ts...>
  589. {
  590. using type = C<Ts...>;
  591. };
  592. template <typename T, template <T...> class, T...>
  593. struct defer_i_
  594. {
  595. };
  596. template <typename T, template <T...> class C, T... Is>
  597. requires valid_i<T, C, Is...> struct defer_i_<T, C, Is...>
  598. {
  599. using type = C<Is...>;
  600. };
  601. #elif defined(META_WORKAROUND_MSVC_703656) // ^^^ Concepts / MSVC workaround vvv
  602. template <typename, template <typename...> class, typename...>
  603. struct _defer_
  604. {
  605. };
  606. template <template <typename...> class C, typename... Ts>
  607. struct _defer_<void_<C<Ts...>>, C, Ts...>
  608. {
  609. using type = C<Ts...>;
  610. };
  611. template <template <typename...> class C, typename... Ts>
  612. using defer_ = _defer_<void, C, Ts...>;
  613. template <typename, typename T, template <T...> class, T...>
  614. struct _defer_i_
  615. {
  616. };
  617. template <typename T, template <T...> class C, T... Is>
  618. struct _defer_i_<void_<C<Is...>>, T, C, Is...>
  619. {
  620. using type = C<Is...>;
  621. };
  622. template <typename T, template <T...> class C, T... Is>
  623. using defer_i_ = _defer_i_<void, T, C, Is...>;
  624. #else // ^^^ workaround ^^^ / vvv no workaround vvv
  625. template <template <typename...> class C, typename... Ts,
  626. template <typename...> class D = C>
  627. id<D<Ts...>> try_defer_(int);
  628. template <template <typename...> class C, typename... Ts>
  629. nil_ try_defer_(long);
  630. template <template <typename...> class C, typename... Ts>
  631. using defer_ = decltype(detail::try_defer_<C, Ts...>(0));
  632. template <typename T, template <T...> class C, T... Is, template <T...> class D = C>
  633. id<D<Is...>> try_defer_i_(int);
  634. template <typename T, template <T...> class C, T... Is>
  635. nil_ try_defer_i_(long);
  636. template <typename T, template <T...> class C, T... Is>
  637. using defer_i_ = decltype(detail::try_defer_i_<T, C, Is...>(0));
  638. #endif // Concepts vs. MSVC vs. Other
  639. template <typename T>
  640. using _t_t = _t<_t<T>>;
  641. } // namespace detail
  642. /// \endcond
  643. ///////////////////////////////////////////////////////////////////////////////////////////
  644. // defer
  645. /// A wrapper that defers the instantiation of a template \p C with type parameters \p Ts in
  646. /// a \c lambda or \c let expression.
  647. ///
  648. /// In the code below, the lambda would ideally be written as
  649. /// `lambda<_a,_b,push_back<_a,_b>>`, however this fails since `push_back` expects its first
  650. /// argument to be a list, not a placeholder. Instead, we express it using \c defer as
  651. /// follows:
  652. ///
  653. /// \code
  654. /// template <typename L>
  655. /// using reverse = reverse_fold<L, list<>, lambda<_a, _b, defer<push_back, _a, _b>>>;
  656. /// \endcode
  657. ///
  658. /// \ingroup invocation
  659. template <template <typename...> class C, typename... Ts>
  660. struct defer : detail::defer_<C, Ts...>
  661. {
  662. };
  663. ///////////////////////////////////////////////////////////////////////////////////////////
  664. // defer_i
  665. /// A wrapper that defers the instantiation of a template \p C with integral constant
  666. /// parameters \p Is in a \c lambda or \c let expression.
  667. /// \sa `defer`
  668. /// \ingroup invocation
  669. template <typename T, template <T...> class C, T... Is>
  670. struct defer_i : detail::defer_i_<T, C, Is...>
  671. {
  672. };
  673. ///////////////////////////////////////////////////////////////////////////////////////////
  674. // defer_trait
  675. /// A wrapper that defers the instantiation of a trait \p C with type parameters \p Ts in a
  676. /// \c lambda or \c let expression.
  677. /// \sa `defer`
  678. /// \ingroup invocation
  679. template <template <typename...> class C, typename... Ts>
  680. using defer_trait = defer<detail::_t_t, detail::defer_<C, Ts...>>;
  681. ///////////////////////////////////////////////////////////////////////////////////////////
  682. // defer_trait_i
  683. /// A wrapper that defers the instantiation of a trait \p C with integral constant
  684. /// parameters \p Is in a \c lambda or \c let expression.
  685. /// \sa `defer_i`
  686. /// \ingroup invocation
  687. template <typename T, template <T...> class C, T... Is>
  688. using defer_trait_i = defer<detail::_t_t, detail::defer_i_<T, C, Is...>>;
  689. /// An alias that computes the size of the type \p T.
  690. /// \par Complexity
  691. /// \f$ O(1) \f$.
  692. /// \ingroup trait
  693. template <typename T>
  694. using sizeof_ = meta::size_t<sizeof(T)>;
  695. /// An alias that computes the alignment required for any instance of the type \p T.
  696. /// \par Complexity
  697. /// \f$ O(1) \f$.
  698. /// \ingroup trait
  699. template <typename T>
  700. using alignof_ = meta::size_t<alignof(T)>;
  701. namespace lazy
  702. {
  703. /// \sa `meta::sizeof_`
  704. /// \ingroup lazy_trait
  705. template <typename T>
  706. using sizeof_ = defer<sizeof_, T>;
  707. /// \sa `meta::alignof_`
  708. /// \ingroup lazy_trait
  709. template <typename T>
  710. using alignof_ = defer<alignof_, T>;
  711. } // namespace lazy
  712. #if META_CXX_VARIABLE_TEMPLATES
  713. /// is
  714. /// Test whether a type \p T is an instantiation of class
  715. /// template \p C.
  716. /// \ingroup trait
  717. template <typename T, template <typename...> class C>
  718. using is = bool_<is_v<T, C>>;
  719. #else
  720. /// is
  721. /// \cond
  722. namespace detail
  723. {
  724. template <typename, template <typename...> class>
  725. struct is_ : std::false_type
  726. {
  727. };
  728. template <typename... Ts, template <typename...> class C>
  729. struct is_<C<Ts...>, C> : std::true_type
  730. {
  731. };
  732. } // namespace detail
  733. /// \endcond
  734. /// Test whether a type \c T is an instantiation of class
  735. /// template \c C.
  736. /// \ingroup trait
  737. template <typename T, template <typename...> class C>
  738. using is = _t<detail::is_<T, C>>;
  739. #endif
  740. /// Compose the Invocables \p Fns in the parameter pack \p Ts.
  741. /// \ingroup composition
  742. template <META_TYPE_CONSTRAINT(invocable)... Fns>
  743. struct compose_
  744. {
  745. };
  746. template <META_TYPE_CONSTRAINT(invocable) Fn0>
  747. struct compose_<Fn0>
  748. {
  749. template <typename... Ts>
  750. using invoke = invoke<Fn0, Ts...>;
  751. };
  752. template <META_TYPE_CONSTRAINT(invocable) Fn0, META_TYPE_CONSTRAINT(invocable)... Fns>
  753. struct compose_<Fn0, Fns...>
  754. {
  755. template <typename... Ts>
  756. using invoke = invoke<Fn0, invoke<compose_<Fns...>, Ts...>>;
  757. };
  758. template <typename... Fns>
  759. using compose = compose_<Fns...>;
  760. namespace lazy
  761. {
  762. /// \sa 'meta::compose'
  763. /// \ingroup lazy_composition
  764. template <typename... Fns>
  765. using compose = defer<compose, Fns...>;
  766. } // namespace lazy
  767. /// Turn a template \p C into an invocable.
  768. /// \ingroup composition
  769. template <template <typename...> class C>
  770. struct quote
  771. {
  772. // Indirection through defer here needed to avoid Core issue 1430
  773. // https://wg21.link/cwg1430
  774. template <typename... Ts>
  775. using invoke = _t<defer<C, Ts...>>;
  776. };
  777. /// Turn a template \p C taking literals of type \p T into a
  778. /// invocable.
  779. /// \ingroup composition
  780. template <typename T, template <T...> class C>
  781. struct quote_i
  782. {
  783. // Indirection through defer_i here needed to avoid Core issue 1430
  784. // https://wg21.link/cwg1430
  785. template <META_TYPE_CONSTRAINT(integral)... Ts>
  786. using invoke = _t<defer_i<T, C, Ts::type::value...>>;
  787. };
  788. #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 8 && \
  789. !defined(META_DOXYGEN_INVOKED)
  790. template <template <typename...> class C>
  791. struct quote_trait
  792. {
  793. template <typename... Ts>
  794. using invoke = _t<invoke<quote<C>, Ts...>>;
  795. };
  796. template <typename T, template <T...> class C>
  797. struct quote_trait_i
  798. {
  799. template <typename... Ts>
  800. using invoke = _t<invoke<quote_i<T, C>, Ts...>>;
  801. };
  802. #else
  803. // clang-format off
  804. /// Turn a trait template \p C into an invocable.
  805. /// \code
  806. /// static_assert(std::is_same_v<invoke<quote_trait<std::add_const>, int>, int const>, "");
  807. /// \endcode
  808. /// \ingroup composition
  809. template <template <typename...> class C>
  810. using quote_trait = compose<quote<_t>, quote<C>>;
  811. /// Turn a trait template \p C taking literals of type \p T into an invocable.
  812. /// \ingroup composition
  813. template <typename T, template <T...> class C>
  814. using quote_trait_i = compose<quote<_t>, quote_i<T, C>>;
  815. // clang-format on
  816. #endif
  817. /// An invocable that partially applies the invocable
  818. /// \p Fn by binding the arguments \p Ts to the \e front of \p Fn.
  819. /// \ingroup composition
  820. template <META_TYPE_CONSTRAINT(invocable) Fn, typename... Ts>
  821. struct bind_front
  822. {
  823. template <typename... Us>
  824. using invoke = invoke<Fn, Ts..., Us...>;
  825. };
  826. /// An invocable that partially applies the invocable \p Fn by binding the
  827. /// arguments \p Us to the \e back of \p Fn.
  828. /// \ingroup composition
  829. template <META_TYPE_CONSTRAINT(invocable) Fn, typename... Us>
  830. struct bind_back
  831. {
  832. template <typename... Ts>
  833. using invoke = invoke<Fn, Ts..., Us...>;
  834. };
  835. namespace lazy
  836. {
  837. /// \sa 'meta::bind_front'
  838. /// \ingroup lazy_composition
  839. template <typename Fn, typename... Ts>
  840. using bind_front = defer<bind_front, Fn, Ts...>;
  841. /// \sa 'meta::bind_back'
  842. /// \ingroup lazy_composition
  843. template <typename Fn, typename... Ts>
  844. using bind_back = defer<bind_back, Fn, Ts...>;
  845. } // namespace lazy
  846. /// Extend meta with your own datatypes.
  847. namespace extension
  848. {
  849. /// A trait that unpacks the types in the type list \p L into the invocable
  850. /// \p Fn.
  851. /// \ingroup extension
  852. template <META_TYPE_CONSTRAINT(invocable) Fn, typename L>
  853. struct apply
  854. {
  855. };
  856. template <META_TYPE_CONSTRAINT(invocable) Fn, typename Ret, typename... Args>
  857. struct apply<Fn, Ret(Args...)> : lazy::invoke<Fn, Ret, Args...>
  858. {
  859. };
  860. template <META_TYPE_CONSTRAINT(invocable) Fn, template <typename...> class T,
  861. typename... Ts>
  862. struct apply<Fn, T<Ts...>> : lazy::invoke<Fn, Ts...>
  863. {
  864. };
  865. template <META_TYPE_CONSTRAINT(invocable) Fn, typename T, T... Is>
  866. struct apply<Fn, integer_sequence<T, Is...>>
  867. : lazy::invoke<Fn, std::integral_constant<T, Is>...>
  868. {
  869. };
  870. } // namespace extension
  871. /// Applies the invocable \p Fn using the types in the type list \p L as
  872. /// arguments.
  873. /// \ingroup invocation
  874. template <META_TYPE_CONSTRAINT(invocable) Fn, typename L>
  875. using apply = _t<extension::apply<Fn, L>>;
  876. namespace lazy
  877. {
  878. template <typename Fn, typename L>
  879. using apply = defer<apply, Fn, L>;
  880. }
  881. /// An invocable that takes a bunch of arguments, bundles them into a type
  882. /// list, and then calls the invocable \p Fn with the type list \p Q.
  883. /// \ingroup composition
  884. template <META_TYPE_CONSTRAINT(invocable) Fn,
  885. META_TYPE_CONSTRAINT(invocable) Q = quote<list>>
  886. using curry = compose<Fn, Q>;
  887. /// An invocable that takes a type list, unpacks the types, and then
  888. /// calls the invocable \p Fn with the types.
  889. /// \ingroup composition
  890. template <META_TYPE_CONSTRAINT(invocable) Fn>
  891. using uncurry = bind_front<quote<apply>, Fn>;
  892. namespace lazy
  893. {
  894. /// \sa 'meta::curry'
  895. /// \ingroup lazy_composition
  896. template <typename Fn, typename Q = quote<list>>
  897. using curry = defer<curry, Fn, Q>;
  898. /// \sa 'meta::uncurry'
  899. /// \ingroup lazy_composition
  900. template <typename Fn>
  901. using uncurry = defer<uncurry, Fn>;
  902. } // namespace lazy
  903. /// An invocable that reverses the order of the first two arguments.
  904. /// \ingroup composition
  905. template <META_TYPE_CONSTRAINT(invocable) Fn>
  906. struct flip
  907. {
  908. private:
  909. template <typename... Ts>
  910. struct impl
  911. {
  912. };
  913. template <typename A, typename B, typename... Ts>
  914. struct impl<A, B, Ts...> : lazy::invoke<Fn, B, A, Ts...>
  915. {
  916. };
  917. public:
  918. template <typename... Ts>
  919. using invoke = _t<impl<Ts...>>;
  920. };
  921. namespace lazy
  922. {
  923. /// \sa 'meta::flip'
  924. /// \ingroup lazy_composition
  925. template <typename Fn>
  926. using flip = defer<flip, Fn>;
  927. } // namespace lazy
  928. /// \cond
  929. namespace detail
  930. {
  931. template <typename...>
  932. struct on_
  933. {
  934. };
  935. template <typename Fn, typename... Gs>
  936. struct on_<Fn, Gs...>
  937. {
  938. template <typename... Ts>
  939. using invoke = invoke<Fn, invoke<compose<Gs...>, Ts>...>;
  940. };
  941. } // namespace detail
  942. /// \endcond
  943. /// Use as `on<Fn, Gs...>`. Creates an invocable that applies invocable \c Fn to the
  944. /// result of applying invocable `compose<Gs...>` to all the arguments.
  945. /// \ingroup composition
  946. template <META_TYPE_CONSTRAINT(invocable)... Fns>
  947. using on_ = detail::on_<Fns...>;
  948. template <typename... Fns>
  949. using on = on_<Fns...>;
  950. namespace lazy
  951. {
  952. /// \sa 'meta::on'
  953. /// \ingroup lazy_composition
  954. template <typename Fn, typename G>
  955. using on = defer<on, Fn, G>;
  956. } // namespace lazy
  957. ///////////////////////////////////////////////////////////////////////////////////////////
  958. // if_
  959. /// \cond
  960. namespace detail
  961. {
  962. #ifdef META_CONCEPT
  963. template <typename...>
  964. struct _if_
  965. {
  966. };
  967. template <integral If>
  968. struct _if_<If> : std::enable_if<_v<If>>
  969. {
  970. };
  971. template <integral If, typename Then>
  972. struct _if_<If, Then> : std::enable_if<_v<If>, Then>
  973. {
  974. };
  975. template <integral If, typename Then, typename Else>
  976. struct _if_<If, Then, Else> : std::conditional<_v<If>, Then, Else>
  977. {
  978. };
  979. #elif defined(__clang__)
  980. // Clang is faster with this implementation
  981. template <typename, typename = bool>
  982. struct _if_
  983. {
  984. };
  985. template <typename If>
  986. struct _if_<list<If>, decltype(bool(If::type::value))> : std::enable_if<If::type::value>
  987. {
  988. };
  989. template <typename If, typename Then>
  990. struct _if_<list<If, Then>, decltype(bool(If::type::value))>
  991. : std::enable_if<If::type::value, Then>
  992. {
  993. };
  994. template <typename If, typename Then, typename Else>
  995. struct _if_<list<If, Then, Else>, decltype(bool(If::type::value))>
  996. : std::conditional<If::type::value, Then, Else>
  997. {
  998. };
  999. #else
  1000. // GCC seems to prefer this implementation
  1001. template <typename, typename = std::true_type>
  1002. struct _if_
  1003. {
  1004. };
  1005. template <typename If>
  1006. struct _if_<list<If>, bool_<If::type::value>>
  1007. {
  1008. using type = void;
  1009. };
  1010. template <typename If, typename Then>
  1011. struct _if_<list<If, Then>, bool_<If::type::value>>
  1012. {
  1013. using type = Then;
  1014. };
  1015. template <typename If, typename Then, typename Else>
  1016. struct _if_<list<If, Then, Else>, bool_<If::type::value>>
  1017. {
  1018. using type = Then;
  1019. };
  1020. template <typename If, typename Then, typename Else>
  1021. struct _if_<list<If, Then, Else>, bool_<!If::type::value>>
  1022. {
  1023. using type = Else;
  1024. };
  1025. #endif
  1026. } // namespace detail
  1027. /// \endcond
  1028. /// Select one type or another depending on a compile-time Boolean.
  1029. /// \ingroup logical
  1030. #ifdef META_CONCEPT
  1031. template <typename... Args>
  1032. using if_ = _t<detail::_if_<Args...>>;
  1033. /// Select one type or another depending on a compile-time Boolean.
  1034. /// \ingroup logical
  1035. template <bool If, typename... Args>
  1036. using if_c = _t<detail::_if_<bool_<If>, Args...>>;
  1037. #else
  1038. template <typename... Args>
  1039. using if_ = _t<detail::_if_<list<Args...>>>;
  1040. template <bool If, typename... Args>
  1041. using if_c = _t<detail::_if_<list<bool_<If>, Args...>>>;
  1042. #endif
  1043. namespace lazy
  1044. {
  1045. /// \sa 'meta::if_'
  1046. /// \ingroup lazy_logical
  1047. template <typename... Args>
  1048. using if_ = defer<if_, Args...>;
  1049. /// \sa 'meta::if_c'
  1050. /// \ingroup lazy_logical
  1051. template <bool If, typename... Args>
  1052. using if_c = if_<bool_<If>, Args...>;
  1053. } // namespace lazy
  1054. /// \cond
  1055. namespace detail
  1056. {
  1057. #ifdef META_CONCEPT
  1058. template <typename...>
  1059. struct _and_
  1060. {
  1061. };
  1062. template <>
  1063. struct _and_<> : std::true_type
  1064. {
  1065. };
  1066. template <integral B, typename... Bs>
  1067. requires (bool(B::type::value)) struct _and_<B, Bs...> : _and_<Bs...>
  1068. {
  1069. };
  1070. template <integral B, typename... Bs>
  1071. requires (!bool(B::type::value)) struct _and_<B, Bs...> : std::false_type
  1072. {
  1073. };
  1074. template <typename...>
  1075. struct _or_
  1076. {
  1077. };
  1078. template <>
  1079. struct _or_<> : std::false_type
  1080. {
  1081. };
  1082. template <integral B, typename... Bs>
  1083. requires (bool(B::type::value)) struct _or_<B, Bs...> : std::true_type
  1084. {
  1085. };
  1086. template <integral B, typename... Bs>
  1087. requires (!bool(B::type::value)) struct _or_<B, Bs...> : _or_<Bs...>
  1088. {
  1089. };
  1090. #else
  1091. template <bool>
  1092. struct _and_
  1093. {
  1094. template <typename...>
  1095. using invoke = std::true_type;
  1096. };
  1097. template <>
  1098. struct _and_<false>
  1099. {
  1100. template <typename B, typename... Bs>
  1101. using invoke = invoke<
  1102. if_c<!B::type::value, id<std::false_type>, _and_<0 == sizeof...(Bs)>>,
  1103. Bs...>;
  1104. };
  1105. template <bool>
  1106. struct _or_
  1107. {
  1108. template <typename = void>
  1109. using invoke = std::false_type;
  1110. };
  1111. template <>
  1112. struct _or_<false>
  1113. {
  1114. template <typename B, typename... Bs>
  1115. using invoke = invoke<
  1116. if_c<B::type::value, id<std::true_type>, _or_<0 == sizeof...(Bs)>>,
  1117. Bs...>;
  1118. };
  1119. #endif
  1120. } // namespace detail
  1121. /// \endcond
  1122. /// Logically negate the Boolean parameter
  1123. /// \ingroup logical
  1124. template <bool B>
  1125. using not_c = bool_<!B>;
  1126. /// Logically negate the integral constant-wrapped Boolean parameter.
  1127. /// \ingroup logical
  1128. template <META_TYPE_CONSTRAINT(integral) B>
  1129. using not_ = not_c<B::type::value>;
  1130. #if META_CXX_FOLD_EXPRESSIONS && !defined(META_WORKAROUND_GCC_UNKNOWN1)
  1131. template <bool... Bs>
  1132. META_INLINE_VAR constexpr bool and_v = (true && ... && Bs);
  1133. /// Logically AND together all the Boolean parameters
  1134. /// \ingroup logical
  1135. template <bool... Bs>
  1136. #if defined(META_WORKAROUND_MSVC_756112) || defined(META_WORKAROUND_GCC_86356)
  1137. using and_c = bool_<and_v<Bs...>>;
  1138. #else
  1139. using and_c = bool_<(true && ... && Bs)>;
  1140. #endif
  1141. #else
  1142. #if defined(META_WORKAROUND_GCC_66405)
  1143. template <bool... Bs>
  1144. using and_c = meta::bool_<
  1145. META_IS_SAME(integer_sequence<bool, true, Bs...>,
  1146. integer_sequence<bool, Bs..., true>)>;
  1147. #else
  1148. template <bool... Bs>
  1149. struct and_c
  1150. : meta::bool_<
  1151. META_IS_SAME(integer_sequence<bool, Bs...>,
  1152. integer_sequence<bool, (Bs || true)...>)>
  1153. {};
  1154. #endif
  1155. #if META_CXX_VARIABLE_TEMPLATES
  1156. template <bool... Bs>
  1157. META_INLINE_VAR constexpr bool and_v =
  1158. META_IS_SAME(integer_sequence<bool, Bs...>,
  1159. integer_sequence<bool, (Bs || true)...>);
  1160. #endif
  1161. #endif
  1162. /// Logically AND together all the integral constant-wrapped Boolean
  1163. /// parameters, \e without short-circuiting.
  1164. /// \ingroup logical
  1165. template <META_TYPE_CONSTRAINT(integral)... Bs>
  1166. using strict_and_ = and_c<Bs::type::value...>;
  1167. template <typename... Bs>
  1168. using strict_and = strict_and_<Bs...>;
  1169. /// Logically AND together all the integral constant-wrapped Boolean
  1170. /// parameters, \e with short-circuiting.
  1171. /// \ingroup logical
  1172. template <typename... Bs>
  1173. #ifdef META_CONCEPT
  1174. using and_ = _t<detail::_and_<Bs...>>;
  1175. #else
  1176. // Make a trip through defer<> to avoid CWG1430
  1177. // https://wg21.link/cwg1430
  1178. using and_ = _t<defer<detail::_and_<0 == sizeof...(Bs)>::template invoke, Bs...>>;
  1179. #endif
  1180. /// Logically OR together all the Boolean parameters
  1181. /// \ingroup logical
  1182. #if META_CXX_FOLD_EXPRESSIONS && !defined(META_WORKAROUND_GCC_UNKNOWN1)
  1183. template <bool... Bs>
  1184. META_INLINE_VAR constexpr bool or_v = (false || ... || Bs);
  1185. template <bool... Bs>
  1186. #if defined(META_WORKAROUND_MSVC_756112) || defined(META_WORKAROUND_GCC_86356)
  1187. using or_c = bool_<or_v<Bs...>>;
  1188. #else
  1189. using or_c = bool_<(false || ... || Bs)>;
  1190. #endif
  1191. #else
  1192. template <bool... Bs>
  1193. struct or_c
  1194. : meta::bool_<
  1195. !META_IS_SAME(integer_sequence<bool, Bs...>,
  1196. integer_sequence<bool, (Bs && false)...>)>
  1197. {};
  1198. #if META_CXX_VARIABLE_TEMPLATES
  1199. template <bool... Bs>
  1200. META_INLINE_VAR constexpr bool or_v =
  1201. !META_IS_SAME(integer_sequence<bool, Bs...>,
  1202. integer_sequence<bool, (Bs && false)...>);
  1203. #endif
  1204. #endif
  1205. /// Logically OR together all the integral constant-wrapped Boolean
  1206. /// parameters, \e without short-circuiting.
  1207. /// \ingroup logical
  1208. template <META_TYPE_CONSTRAINT(integral)... Bs>
  1209. using strict_or_ = or_c<Bs::type::value...>;
  1210. template <typename... Bs>
  1211. using strict_or = strict_or_<Bs...>;
  1212. /// Logically OR together all the integral constant-wrapped Boolean
  1213. /// parameters, \e with short-circuiting.
  1214. /// \ingroup logical
  1215. template <typename... Bs>
  1216. #ifdef META_CONCEPT
  1217. using or_ = _t<detail::_or_<Bs...>>;
  1218. #else
  1219. // Make a trip through defer<> to avoid CWG1430
  1220. // https://wg21.link/cwg1430
  1221. using or_ = _t<defer<detail::_or_<0 == sizeof...(Bs)>::template invoke, Bs...>>;
  1222. #endif
  1223. namespace lazy
  1224. {
  1225. /// \sa 'meta::and_'
  1226. /// \ingroup lazy_logical
  1227. template <typename... Bs>
  1228. using and_ = defer<and_, Bs...>;
  1229. /// \sa 'meta::or_'
  1230. /// \ingroup lazy_logical
  1231. template <typename... Bs>
  1232. using or_ = defer<or_, Bs...>;
  1233. /// \sa 'meta::not_'
  1234. /// \ingroup lazy_logical
  1235. template <typename B>
  1236. using not_ = defer<not_, B>;
  1237. /// \sa 'meta::strict_and'
  1238. /// \ingroup lazy_logical
  1239. template <typename... Bs>
  1240. using strict_and = defer<strict_and, Bs...>;
  1241. /// \sa 'meta::strict_or'
  1242. /// \ingroup lazy_logical
  1243. template <typename... Bs>
  1244. using strict_or = defer<strict_or, Bs...>;
  1245. } // namespace lazy
  1246. ///////////////////////////////////////////////////////////////////////////////////////////
  1247. // fold
  1248. /// \cond
  1249. namespace detail
  1250. {
  1251. template <typename, typename, typename>
  1252. struct fold_
  1253. {
  1254. };
  1255. template <typename Fn, typename T0, typename T1, typename T2, typename T3, typename T4,
  1256. typename T5, typename T6, typename T7, typename T8, typename T9>
  1257. struct compose10_
  1258. {
  1259. template <typename X, typename Y>
  1260. using F = invoke<Fn, X, Y>;
  1261. template <typename S>
  1262. using invoke =
  1263. F<F<F<F<F<F<F<F<F<F<_t<S>, T0>, T1>, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>;
  1264. };
  1265. #ifdef META_CONCEPT
  1266. template <typename Fn>
  1267. struct compose_
  1268. {
  1269. template <typename X, typename Y>
  1270. using F = invoke<Fn, X, Y>;
  1271. template <typename T0, typename T1, typename T2, typename T3, typename T4,
  1272. typename T5, typename T6, typename T7, typename T8, typename T9,
  1273. typename State>
  1274. using invoke =
  1275. F<F<F<F<F<F<F<F<F<F<State, T0>, T1>, T2>, T3>, T4>, T5>, T6>, T7>, T8>, T9>;
  1276. };
  1277. template <typename State, typename Fn>
  1278. struct fold_<list<>, State, Fn>
  1279. {
  1280. using type = State;
  1281. };
  1282. template <typename Head, typename... Tail, typename State, typename Fn>
  1283. requires valid<invoke, Fn, State, Head>
  1284. struct fold_<list<Head, Tail...>, State, Fn>
  1285. : fold_<list<Tail...>, invoke<Fn, State, Head>, Fn>
  1286. {
  1287. };
  1288. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
  1289. typename T6, typename T7, typename T8, typename T9, typename... Tail,
  1290. typename State, typename Fn>
  1291. requires valid<invoke, compose_<Fn>, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, State>
  1292. struct fold_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Tail...>, State, Fn>
  1293. : fold_<list<Tail...>,
  1294. invoke<compose_<Fn>, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, State>, Fn>
  1295. {
  1296. };
  1297. #else // ^^^ Concepts / no Concepts vvv
  1298. template <typename Fn, typename T0>
  1299. struct compose1_
  1300. {
  1301. template <typename X>
  1302. using invoke = invoke<Fn, _t<X>, T0>;
  1303. };
  1304. template <typename State, typename Fn>
  1305. struct fold_<list<>, State, Fn> : State
  1306. {
  1307. };
  1308. template <typename Head, typename... Tail, typename State, typename Fn>
  1309. struct fold_<list<Head, Tail...>, State, Fn>
  1310. : fold_<list<Tail...>, lazy::invoke<compose1_<Fn, Head>, State>, Fn>
  1311. {
  1312. };
  1313. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
  1314. typename T6, typename T7, typename T8, typename T9, typename... Tail,
  1315. typename State, typename Fn>
  1316. struct fold_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Tail...>, State, Fn>
  1317. : fold_<list<Tail...>,
  1318. lazy::invoke<compose10_<Fn, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>, State>, Fn>
  1319. {
  1320. };
  1321. #endif // META_CONCEPT
  1322. } // namespace detail
  1323. /// \endcond
  1324. /// Return a new \c meta::list constructed by doing a left fold of the list \p L using
  1325. /// binary invocable \p Fn and initial state \p State. That is, the \c State_N for
  1326. /// the list element \c A_N is computed by `Fn(State_N-1, A_N) -> State_N`.
  1327. /// \par Complexity
  1328. /// \f$ O(N) \f$.
  1329. /// \ingroup transformation
  1330. template <META_TYPE_CONSTRAINT(list_like) L, typename State, META_TYPE_CONSTRAINT(invocable) Fn>
  1331. #ifdef META_CONCEPT
  1332. using fold = _t<detail::fold_<L, State, Fn>>;
  1333. #else
  1334. using fold = _t<detail::fold_<L, id<State>, Fn>>;
  1335. #endif
  1336. /// An alias for `meta::fold`.
  1337. /// \par Complexity
  1338. /// \f$ O(N) \f$.
  1339. /// \ingroup transformation
  1340. template <META_TYPE_CONSTRAINT(list_like) L, typename State, META_TYPE_CONSTRAINT(invocable) Fn>
  1341. using accumulate = fold<L, State, Fn>;
  1342. namespace lazy
  1343. {
  1344. /// \sa 'meta::foldl'
  1345. /// \ingroup lazy_transformation
  1346. template <typename L, typename State, typename Fn>
  1347. using fold = defer<fold, L, State, Fn>;
  1348. /// \sa 'meta::accumulate'
  1349. /// \ingroup lazy_transformation
  1350. template <typename L, typename State, typename Fn>
  1351. using accumulate = defer<accumulate, L, State, Fn>;
  1352. } // namespace lazy
  1353. ///////////////////////////////////////////////////////////////////////////////////////////
  1354. // reverse_fold
  1355. /// \cond
  1356. namespace detail
  1357. {
  1358. template <typename, typename, typename>
  1359. struct reverse_fold_
  1360. {
  1361. };
  1362. template <typename State, typename Fn>
  1363. struct reverse_fold_<list<>, State, Fn>
  1364. {
  1365. using type = State;
  1366. };
  1367. #ifdef META_CONCEPT
  1368. template <typename Head, typename... L, typename State, typename Fn>
  1369. requires trait<reverse_fold_<list<L...>, State, Fn>> struct reverse_fold_<
  1370. list<Head, L...>, State, Fn>
  1371. : lazy::invoke<Fn, _t<reverse_fold_<list<L...>, State, Fn>>, Head>
  1372. {
  1373. };
  1374. #else
  1375. template <typename Head, typename... Tail, typename State, typename Fn>
  1376. struct reverse_fold_<list<Head, Tail...>, State, Fn>
  1377. : lazy::invoke<compose1_<Fn, Head>, reverse_fold_<list<Tail...>, State, Fn>>
  1378. {
  1379. };
  1380. #endif
  1381. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
  1382. typename T6, typename T7, typename T8, typename T9, typename... Tail,
  1383. typename State, typename Fn>
  1384. struct reverse_fold_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Tail...>, State, Fn>
  1385. : lazy::invoke<compose10_<Fn, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0>,
  1386. reverse_fold_<list<Tail...>, State, Fn>>
  1387. {
  1388. };
  1389. } // namespace detail
  1390. /// \endcond
  1391. /// Return a new \c meta::list constructed by doing a right fold of the list \p L using
  1392. /// binary invocable \p Fn and initial state \p State. That is, the \c State_N for the list
  1393. /// element \c A_N is computed by `Fn(A_N, State_N+1) -> State_N`.
  1394. /// \par Complexity
  1395. /// \f$ O(N) \f$.
  1396. /// \ingroup transformation
  1397. template <META_TYPE_CONSTRAINT(list_like) L, typename State, META_TYPE_CONSTRAINT(invocable) Fn>
  1398. using reverse_fold = _t<detail::reverse_fold_<L, State, Fn>>;
  1399. namespace lazy
  1400. {
  1401. /// \sa 'meta::foldr'
  1402. /// \ingroup lazy_transformation
  1403. template <typename L, typename State, typename Fn>
  1404. using reverse_fold = defer<reverse_fold, L, State, Fn>;
  1405. } // namespace lazy
  1406. ///////////////////////////////////////////////////////////////////////////////////////////
  1407. // npos
  1408. /// A special value used to indicate no matches. It equals the maximum
  1409. /// value representable by std::size_t.
  1410. /// \ingroup list
  1411. using npos = meta::size_t<std::size_t(-1)>;
  1412. ///////////////////////////////////////////////////////////////////////////////////////////
  1413. // list
  1414. /// A list of types.
  1415. /// \ingroup list
  1416. template <typename... Ts>
  1417. struct list
  1418. {
  1419. using type = list;
  1420. /// \return `sizeof...(Ts)`
  1421. static constexpr std::size_t size() noexcept { return sizeof...(Ts); }
  1422. };
  1423. ///////////////////////////////////////////////////////////////////////////////////////////
  1424. // size
  1425. /// An integral constant wrapper that is the size of the \c meta::list
  1426. /// \p L.
  1427. /// \ingroup list
  1428. template <META_TYPE_CONSTRAINT(list_like) L>
  1429. using size = meta::size_t<L::size()>;
  1430. namespace lazy
  1431. {
  1432. /// \sa 'meta::size'
  1433. /// \ingroup lazy_list
  1434. template <typename L>
  1435. using size = defer<size, L>;
  1436. } // namespace lazy
  1437. ///////////////////////////////////////////////////////////////////////////////////////////
  1438. // concat
  1439. /// \cond
  1440. namespace detail
  1441. {
  1442. template <typename... Lists>
  1443. struct concat_
  1444. {
  1445. };
  1446. template <>
  1447. struct concat_<>
  1448. {
  1449. using type = list<>;
  1450. };
  1451. template <typename... L1>
  1452. struct concat_<list<L1...>>
  1453. {
  1454. using type = list<L1...>;
  1455. };
  1456. template <typename... L1, typename... L2>
  1457. struct concat_<list<L1...>, list<L2...>>
  1458. {
  1459. using type = list<L1..., L2...>;
  1460. };
  1461. template <typename... L1, typename... L2, typename... L3>
  1462. struct concat_<list<L1...>, list<L2...>, list<L3...>>
  1463. {
  1464. using type = list<L1..., L2..., L3...>;
  1465. };
  1466. template <typename... L1, typename... L2, typename... L3, typename... Rest>
  1467. struct concat_<list<L1...>, list<L2...>, list<L3...>, Rest...>
  1468. : concat_<list<L1..., L2..., L3...>, Rest...>
  1469. {
  1470. };
  1471. template <typename... L1, typename... L2, typename... L3, typename... L4,
  1472. typename... L5, typename... L6, typename... L7, typename... L8,
  1473. typename... L9, typename... L10, typename... Rest>
  1474. struct concat_<list<L1...>, list<L2...>, list<L3...>, list<L4...>, list<L5...>,
  1475. list<L6...>, list<L7...>, list<L8...>, list<L9...>, list<L10...>,
  1476. Rest...>
  1477. : concat_<list<L1..., L2..., L3..., L4..., L5..., L6..., L7..., L8..., L9..., L10...>,
  1478. Rest...>
  1479. {
  1480. };
  1481. } // namespace detail
  1482. /// \endcond
  1483. /// Concatenates several lists into a single list.
  1484. /// \pre The parameters must all be instantiations of \c meta::list.
  1485. /// \par Complexity
  1486. /// \f$ O(L) \f$ where \f$ L \f$ is the number of lists in the list of lists.
  1487. /// \ingroup transformation
  1488. template <META_TYPE_CONSTRAINT(list_like)... Ls>
  1489. using concat_ = _t<detail::concat_<Ls...>>;
  1490. template <typename... Lists>
  1491. using concat = concat_<Lists...>;
  1492. namespace lazy
  1493. {
  1494. /// \sa 'meta::concat'
  1495. /// \ingroup lazy_transformation
  1496. template <typename... Lists>
  1497. using concat = defer<concat, Lists...>;
  1498. } // namespace lazy
  1499. /// Joins a list of lists into a single list.
  1500. /// \pre The parameter must be an instantiation of \c meta::list\<T...\>
  1501. /// where each \c T is itself an instantiation of \c meta::list.
  1502. /// \par Complexity
  1503. /// \f$ O(L) \f$ where \f$ L \f$ is the number of lists in the list of
  1504. /// lists.
  1505. /// \ingroup transformation
  1506. template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
  1507. using join = apply<quote<concat>, ListOfLists>;
  1508. namespace lazy
  1509. {
  1510. /// \sa 'meta::join'
  1511. /// \ingroup lazy_transformation
  1512. template <typename ListOfLists>
  1513. using join = defer<join, ListOfLists>;
  1514. } // namespace lazy
  1515. ///////////////////////////////////////////////////////////////////////////////////////////
  1516. // transform
  1517. /// \cond
  1518. namespace detail
  1519. {
  1520. #ifdef META_CONCEPT
  1521. template <typename... Args>
  1522. struct transform_
  1523. {
  1524. };
  1525. template <typename... Ts, invocable Fn>
  1526. requires and_v<valid<invoke, Fn, Ts>...>
  1527. struct transform_<list<Ts...>, Fn>
  1528. {
  1529. using type = list<invoke<Fn, Ts>...>;
  1530. };
  1531. template <typename... Ts, typename... Us, invocable Fn>
  1532. requires and_v<valid<invoke, Fn, Ts, Us>...>
  1533. struct transform_<list<Ts...>, list<Us...>, Fn>
  1534. {
  1535. using type = list<invoke<Fn, Ts, Us>...>;
  1536. };
  1537. #else
  1538. template <typename, typename = void>
  1539. struct transform_
  1540. {
  1541. };
  1542. template <typename... Ts, typename Fn>
  1543. struct transform_<list<list<Ts...>, Fn>, void_<invoke<Fn, Ts>...>>
  1544. {
  1545. using type = list<invoke<Fn, Ts>...>;
  1546. };
  1547. template <typename... Ts0, typename... Ts1, typename Fn>
  1548. struct transform_<list<list<Ts0...>, list<Ts1...>, Fn>,
  1549. void_<invoke<Fn, Ts0, Ts1>...>>
  1550. {
  1551. using type = list<invoke<Fn, Ts0, Ts1>...>;
  1552. };
  1553. #endif
  1554. } // namespace detail
  1555. /// \endcond
  1556. /// Return a new \c meta::list constructed by transforming all the
  1557. /// elements in \p L with the unary invocable \p Fn. \c transform can
  1558. /// also be called with two lists of the same length and a binary
  1559. /// invocable, in which case it returns a new list constructed with the
  1560. /// results of calling \c Fn with each element in the lists, pairwise.
  1561. /// \par Complexity
  1562. /// \f$ O(N) \f$.
  1563. /// \ingroup transformation
  1564. #ifdef META_CONCEPT
  1565. template <typename... Args>
  1566. using transform = _t<detail::transform_<Args...>>;
  1567. #else
  1568. template <typename... Args>
  1569. using transform = _t<detail::transform_<list<Args...>>>;
  1570. #endif
  1571. namespace lazy
  1572. {
  1573. /// \sa 'meta::transform'
  1574. /// \ingroup lazy_transformation
  1575. template <typename... Args>
  1576. using transform = defer<transform, Args...>;
  1577. } // namespace lazy
  1578. ///////////////////////////////////////////////////////////////////////////////////////////
  1579. // repeat_n
  1580. /// \cond
  1581. namespace detail
  1582. {
  1583. template <typename T, std::size_t>
  1584. using first_ = T;
  1585. template <typename T, typename Ints>
  1586. struct repeat_n_c_
  1587. {
  1588. };
  1589. template <typename T, std::size_t... Is>
  1590. struct repeat_n_c_<T, index_sequence<Is...>>
  1591. {
  1592. using type = list<first_<T, Is>...>;
  1593. };
  1594. } // namespace detail
  1595. /// \endcond
  1596. /// Generate `list<T,T,T...T>` of size \p N arguments.
  1597. /// \par Complexity
  1598. /// \f$ O(log N) \f$.
  1599. /// \ingroup list
  1600. template <std::size_t N, typename T = void>
  1601. using repeat_n_c = _t<detail::repeat_n_c_<T, make_index_sequence<N>>>;
  1602. /// Generate `list<T,T,T...T>` of size \p N arguments.
  1603. /// \par Complexity
  1604. /// \f$ O(log N) \f$.
  1605. /// \ingroup list
  1606. template <META_TYPE_CONSTRAINT(integral) N, typename T = void>
  1607. using repeat_n = repeat_n_c<N::type::value, T>;
  1608. namespace lazy
  1609. {
  1610. /// \sa 'meta::repeat_n'
  1611. /// \ingroup lazy_list
  1612. template <typename N, typename T = void>
  1613. using repeat_n = defer<repeat_n, N, T>;
  1614. /// \sa 'meta::repeat_n_c'
  1615. /// \ingroup lazy_list
  1616. template <std::size_t N, typename T = void>
  1617. using repeat_n_c = defer<repeat_n, meta::size_t<N>, T>;
  1618. } // namespace lazy
  1619. ///////////////////////////////////////////////////////////////////////////////////////////
  1620. // at
  1621. /// \cond
  1622. namespace detail
  1623. {
  1624. #if META_HAS_TYPE_PACK_ELEMENT && !defined(META_DOXYGEN_INVOKED)
  1625. template <typename L, std::size_t N, typename = void>
  1626. struct at_
  1627. {
  1628. };
  1629. template <typename... Ts, std::size_t N>
  1630. struct at_<list<Ts...>, N, void_<__type_pack_element<N, Ts...>>>
  1631. {
  1632. using type = __type_pack_element<N, Ts...>;
  1633. };
  1634. #else
  1635. template <typename VoidPtrs>
  1636. struct at_impl_;
  1637. template <typename... VoidPtrs>
  1638. struct at_impl_<list<VoidPtrs...>>
  1639. {
  1640. static nil_ eval(...);
  1641. template <typename T, typename... Us>
  1642. static T eval(VoidPtrs..., T *, Us *...);
  1643. };
  1644. template <typename L, std::size_t N>
  1645. struct at_
  1646. {
  1647. };
  1648. template <typename... Ts, std::size_t N>
  1649. struct at_<list<Ts...>, N>
  1650. : decltype(at_impl_<repeat_n_c<N, void *>>::eval(static_cast<id<Ts> *>(nullptr)...))
  1651. {
  1652. };
  1653. #endif // META_HAS_TYPE_PACK_ELEMENT
  1654. } // namespace detail
  1655. /// \endcond
  1656. /// Return the \p N th element in the \c meta::list \p L.
  1657. /// \par Complexity
  1658. /// Amortized \f$ O(1) \f$.
  1659. /// \ingroup list
  1660. template <META_TYPE_CONSTRAINT(list_like) L, std::size_t N>
  1661. using at_c = _t<detail::at_<L, N>>;
  1662. /// Return the \p N th element in the \c meta::list \p L.
  1663. /// \par Complexity
  1664. /// Amortized \f$ O(1) \f$.
  1665. /// \ingroup list
  1666. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(integral) N>
  1667. using at = at_c<L, N::type::value>;
  1668. namespace lazy
  1669. {
  1670. /// \sa 'meta::at'
  1671. /// \ingroup lazy_list
  1672. template <typename L, typename N>
  1673. using at = defer<at, L, N>;
  1674. } // namespace lazy
  1675. ///////////////////////////////////////////////////////////////////////////////////////////
  1676. // drop
  1677. /// \cond
  1678. namespace detail
  1679. {
  1680. ///////////////////////////////////////////////////////////////////////////////////////
  1681. /// drop_impl_
  1682. template <typename VoidPtrs>
  1683. struct drop_impl_
  1684. {
  1685. static nil_ eval(...);
  1686. };
  1687. template <typename... VoidPtrs>
  1688. struct drop_impl_<list<VoidPtrs...>>
  1689. {
  1690. static nil_ eval(...);
  1691. template <typename... Ts>
  1692. static id<list<Ts...>> eval(VoidPtrs..., id<Ts> *...);
  1693. };
  1694. template <>
  1695. struct drop_impl_<list<>>
  1696. {
  1697. template <typename... Ts>
  1698. static id<list<Ts...>> eval(id<Ts> *...);
  1699. };
  1700. template <typename L, std::size_t N>
  1701. struct drop_
  1702. {
  1703. };
  1704. template <typename... Ts, std::size_t N>
  1705. struct drop_<list<Ts...>, N>
  1706. #if META_CXX_VARIABLE_TEMPLATES
  1707. : decltype(drop_impl_<repeat_n_c<N, void *>>::eval(detail::nullptr_v<id<Ts>>...))
  1708. #else
  1709. : decltype(drop_impl_<repeat_n_c<N, void *>>::eval(detail::_nullptr_v<id<Ts>>()...))
  1710. #endif
  1711. {
  1712. };
  1713. } // namespace detail
  1714. /// \endcond
  1715. /// Return a new \c meta::list by removing the first \p N elements from \p L.
  1716. /// \par Complexity
  1717. /// \f$ O(1) \f$.
  1718. /// \ingroup transformation
  1719. template <META_TYPE_CONSTRAINT(list_like) L, std::size_t N>
  1720. using drop_c = _t<detail::drop_<L, N>>;
  1721. /// Return a new \c meta::list by removing the first \p N elements from \p L.
  1722. /// \par Complexity
  1723. /// \f$ O(1) \f$.
  1724. /// \ingroup transformation
  1725. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(integral) N>
  1726. using drop = drop_c<L, N::type::value>;
  1727. namespace lazy
  1728. {
  1729. /// \sa 'meta::drop'
  1730. /// \ingroup lazy_transformation
  1731. template <typename L, typename N>
  1732. using drop = defer<drop, L, N>;
  1733. } // namespace lazy
  1734. ///////////////////////////////////////////////////////////////////////////////////////////
  1735. // front
  1736. /// \cond
  1737. namespace detail
  1738. {
  1739. template <typename L>
  1740. struct front_
  1741. {
  1742. };
  1743. template <typename Head, typename... Tail>
  1744. struct front_<list<Head, Tail...>>
  1745. {
  1746. using type = Head;
  1747. };
  1748. } // namespace detail
  1749. /// \endcond
  1750. /// Return the first element in \c meta::list \p L.
  1751. /// \par Complexity
  1752. /// \f$ O(1) \f$.
  1753. /// \ingroup list
  1754. template <META_TYPE_CONSTRAINT(list_like) L>
  1755. using front = _t<detail::front_<L>>;
  1756. namespace lazy
  1757. {
  1758. /// \sa 'meta::front'
  1759. /// \ingroup lazy_list
  1760. template <typename L>
  1761. using front = defer<front, L>;
  1762. } // namespace lazy
  1763. ///////////////////////////////////////////////////////////////////////////////////////////
  1764. // back
  1765. /// \cond
  1766. namespace detail
  1767. {
  1768. template <typename L>
  1769. struct back_
  1770. {
  1771. };
  1772. template <typename Head, typename... Tail>
  1773. struct back_<list<Head, Tail...>>
  1774. {
  1775. using type = at_c<list<Head, Tail...>, sizeof...(Tail)>;
  1776. };
  1777. } // namespace detail
  1778. /// \endcond
  1779. /// Return the last element in \c meta::list \p L.
  1780. /// \par Complexity
  1781. /// Amortized \f$ O(1) \f$.
  1782. /// \ingroup list
  1783. template <META_TYPE_CONSTRAINT(list_like) L>
  1784. using back = _t<detail::back_<L>>;
  1785. namespace lazy
  1786. {
  1787. /// \sa 'meta::back'
  1788. /// \ingroup lazy_list
  1789. template <typename L>
  1790. using back = defer<back, L>;
  1791. } // namespace lazy
  1792. ///////////////////////////////////////////////////////////////////////////////////////////
  1793. // push_front
  1794. /// Return a new \c meta::list by adding the element \c T to the front of \p L.
  1795. /// \par Complexity
  1796. /// \f$ O(1) \f$.
  1797. /// \ingroup transformation
  1798. template <META_TYPE_CONSTRAINT(list_like) L, typename... Ts>
  1799. using push_front = apply<bind_front<quote<list>, Ts...>, L>;
  1800. namespace lazy
  1801. {
  1802. /// \sa 'meta::push_front'
  1803. /// \ingroup lazy_transformation
  1804. template <typename... Ts>
  1805. using push_front = defer<push_front, Ts...>;
  1806. } // namespace lazy
  1807. ///////////////////////////////////////////////////////////////////////////////////////////
  1808. // pop_front
  1809. /// \cond
  1810. namespace detail
  1811. {
  1812. template <typename L>
  1813. struct pop_front_
  1814. {
  1815. };
  1816. template <typename Head, typename... L>
  1817. struct pop_front_<list<Head, L...>>
  1818. {
  1819. using type = list<L...>;
  1820. };
  1821. } // namespace detail
  1822. /// \endcond
  1823. /// Return a new \c meta::list by removing the first element from the
  1824. /// front of \p L.
  1825. /// \par Complexity
  1826. /// \f$ O(1) \f$.
  1827. /// \ingroup transformation
  1828. template <META_TYPE_CONSTRAINT(list_like) L>
  1829. using pop_front = _t<detail::pop_front_<L>>;
  1830. namespace lazy
  1831. {
  1832. /// \sa 'meta::pop_front'
  1833. /// \ingroup lazy_transformation
  1834. template <typename L>
  1835. using pop_front = defer<pop_front, L>;
  1836. } // namespace lazy
  1837. ///////////////////////////////////////////////////////////////////////////////////////////
  1838. // push_back
  1839. /// Return a new \c meta::list by adding the element \c T to the back of \p L.
  1840. /// \par Complexity
  1841. /// \f$ O(1) \f$.
  1842. /// \note \c pop_back not provided because it cannot be made to meet the
  1843. /// complexity guarantees one would expect.
  1844. /// \ingroup transformation
  1845. template <META_TYPE_CONSTRAINT(list_like) L, typename... Ts>
  1846. using push_back = apply<bind_back<quote<list>, Ts...>, L>;
  1847. namespace lazy
  1848. {
  1849. /// \sa 'meta::push_back'
  1850. /// \ingroup lazy_transformation
  1851. template <typename... Ts>
  1852. using push_back = defer<push_back, Ts...>;
  1853. } // namespace lazy
  1854. /// \cond
  1855. namespace detail
  1856. {
  1857. template <typename T, typename U>
  1858. using min_ = if_<less<U, T>, U, T>;
  1859. template <typename T, typename U>
  1860. using max_ = if_<less<U, T>, T, U>;
  1861. } // namespace detail
  1862. /// \endcond
  1863. /// An integral constant wrapper around the minimum of `Ts::type::value...`
  1864. /// \ingroup math
  1865. template <META_TYPE_CONSTRAINT(integral)... Ts>
  1866. using min_ = fold<pop_front<list<Ts...>>, front<list<Ts...>>, quote<detail::min_>>;
  1867. template <typename... Ts>
  1868. using min = min_<Ts...>;
  1869. /// An integral constant wrapper around the maximum of `Ts::type::value...`
  1870. /// \ingroup math
  1871. template <META_TYPE_CONSTRAINT(integral)... Ts>
  1872. using max_ = fold<pop_front<list<Ts...>>, front<list<Ts...>>, quote<detail::max_>>;
  1873. template <typename... Ts>
  1874. using max = max_<Ts...>;
  1875. namespace lazy
  1876. {
  1877. /// \sa 'meta::min'
  1878. /// \ingroup lazy_math
  1879. template <typename... Ts>
  1880. using min = defer<min, Ts...>;
  1881. /// \sa 'meta::max'
  1882. /// \ingroup lazy_math
  1883. template <typename... Ts>
  1884. using max = defer<max, Ts...>;
  1885. } // namespace lazy
  1886. ///////////////////////////////////////////////////////////////////////////////////////////
  1887. // empty
  1888. /// An Boolean integral constant wrapper around \c true if \p L is an
  1889. /// empty type list; \c false, otherwise.
  1890. /// \par Complexity
  1891. /// \f$ O(1) \f$.
  1892. /// \ingroup list
  1893. template <META_TYPE_CONSTRAINT(list_like) L>
  1894. using empty = bool_<0 == size<L>::type::value>;
  1895. namespace lazy
  1896. {
  1897. /// \sa 'meta::empty'
  1898. /// \ingroup lazy_list
  1899. template <typename L>
  1900. using empty = defer<empty, L>;
  1901. } // namespace lazy
  1902. ///////////////////////////////////////////////////////////////////////////////////////////
  1903. // pair
  1904. /// A list with exactly two elements
  1905. /// \ingroup list
  1906. template <typename F, typename S>
  1907. using pair = list<F, S>;
  1908. /// Retrieve the first element of the \c pair \p Pair
  1909. /// \ingroup list
  1910. template <typename Pair>
  1911. using first = front<Pair>;
  1912. /// Retrieve the first element of the \c pair \p Pair
  1913. /// \ingroup list
  1914. template <typename Pair>
  1915. using second = front<pop_front<Pair>>;
  1916. namespace lazy
  1917. {
  1918. /// \sa 'meta::first'
  1919. /// \ingroup lazy_list
  1920. template <typename Pair>
  1921. using first = defer<first, Pair>;
  1922. /// \sa 'meta::second'
  1923. /// \ingroup lazy_list
  1924. template <typename Pair>
  1925. using second = defer<second, Pair>;
  1926. } // namespace lazy
  1927. ///////////////////////////////////////////////////////////////////////////////////////////
  1928. // find_index
  1929. /// \cond
  1930. namespace detail
  1931. {
  1932. // With thanks to Peter Dimov:
  1933. constexpr std::size_t find_index_i_(bool const *const first, bool const *const last,
  1934. std::size_t N = 0)
  1935. {
  1936. return first == last ? npos::value
  1937. : *first ? N : find_index_i_(first + 1, last, N + 1);
  1938. }
  1939. template <typename L, typename T>
  1940. struct find_index_
  1941. {
  1942. };
  1943. template <typename V>
  1944. struct find_index_<list<>, V>
  1945. {
  1946. using type = npos;
  1947. };
  1948. template <typename... T, typename V>
  1949. struct find_index_<list<T...>, V>
  1950. {
  1951. #ifdef META_WORKAROUND_LLVM_28385
  1952. static constexpr bool s_v[sizeof...(T)] = {META_IS_SAME(T, V)...};
  1953. #else
  1954. static constexpr bool s_v[] = {META_IS_SAME(T, V)...};
  1955. #endif
  1956. using type = size_t<find_index_i_(s_v, s_v + sizeof...(T))>;
  1957. };
  1958. } // namespace detail
  1959. /// \endcond
  1960. /// Finds the index of the first occurrence of the type \p T within the list \p L.
  1961. /// Returns `#meta::npos` if the type \p T was not found.
  1962. /// \par Complexity
  1963. /// \f$ O(N) \f$.
  1964. /// \ingroup query
  1965. /// \sa `meta::npos`
  1966. template <META_TYPE_CONSTRAINT(list_like) L, typename T>
  1967. using find_index = _t<detail::find_index_<L, T>>;
  1968. namespace lazy
  1969. {
  1970. /// \sa 'meta::find_index'
  1971. /// \ingroup lazy_query
  1972. template <typename L, typename T>
  1973. using find_index = defer<find_index, L, T>;
  1974. } // namespace lazy
  1975. ///////////////////////////////////////////////////////////////////////////////////////////
  1976. // reverse_find_index
  1977. /// \cond
  1978. namespace detail
  1979. {
  1980. // With thanks to Peter Dimov:
  1981. constexpr std::size_t reverse_find_index_i_(bool const *const first,
  1982. bool const *const last, std::size_t N)
  1983. {
  1984. return first == last
  1985. ? npos::value
  1986. : *(last - 1) ? N - 1 : reverse_find_index_i_(first, last - 1, N - 1);
  1987. }
  1988. template <typename L, typename T>
  1989. struct reverse_find_index_
  1990. {
  1991. };
  1992. template <typename V>
  1993. struct reverse_find_index_<list<>, V>
  1994. {
  1995. using type = npos;
  1996. };
  1997. template <typename... T, typename V>
  1998. struct reverse_find_index_<list<T...>, V>
  1999. {
  2000. #ifdef META_WORKAROUND_LLVM_28385
  2001. static constexpr bool s_v[sizeof...(T)] = {META_IS_SAME(T, V)...};
  2002. #else
  2003. static constexpr bool s_v[] = {META_IS_SAME(T, V)...};
  2004. #endif
  2005. using type = size_t<reverse_find_index_i_(s_v, s_v + sizeof...(T), sizeof...(T))>;
  2006. };
  2007. } // namespace detail
  2008. /// \endcond
  2009. /// Finds the index of the last occurrence of the type \p T within the
  2010. /// list \p L. Returns `#meta::npos` if the type \p T was not found.
  2011. /// \par Complexity
  2012. /// \f$ O(N) \f$.
  2013. /// \ingroup query
  2014. /// \sa `#meta::npos`
  2015. template <META_TYPE_CONSTRAINT(list_like) L, typename T>
  2016. using reverse_find_index = _t<detail::reverse_find_index_<L, T>>;
  2017. namespace lazy
  2018. {
  2019. /// \sa 'meta::reverse_find_index'
  2020. /// \ingroup lazy_query
  2021. template <typename L, typename T>
  2022. using reverse_find_index = defer<reverse_find_index, L, T>;
  2023. } // namespace lazy
  2024. ////////////////////////////////////////////////////////////////////////////////////
  2025. // find
  2026. /// Return the tail of the list \p L starting at the first occurrence of
  2027. /// \p T, if any such element exists; the empty list, otherwise.
  2028. /// \par Complexity
  2029. /// \f$ O(N) \f$.
  2030. /// \ingroup query
  2031. template <META_TYPE_CONSTRAINT(list_like) L, typename T>
  2032. using find = drop<L, min<find_index<L, T>, size<L>>>;
  2033. namespace lazy
  2034. {
  2035. /// \sa 'meta::find'
  2036. /// \ingroup lazy_query
  2037. template <typename L, typename T>
  2038. using find = defer<find, L, T>;
  2039. } // namespace lazy
  2040. ////////////////////////////////////////////////////////////////////////////////////
  2041. // reverse_find
  2042. /// \cond
  2043. namespace detail
  2044. {
  2045. template <typename L, typename T, typename State = list<>>
  2046. struct reverse_find_
  2047. {
  2048. };
  2049. template <typename T, typename State>
  2050. struct reverse_find_<list<>, T, State>
  2051. {
  2052. using type = State;
  2053. };
  2054. template <typename Head, typename... L, typename T, typename State>
  2055. struct reverse_find_<list<Head, L...>, T, State> : reverse_find_<list<L...>, T, State>
  2056. {
  2057. };
  2058. template <typename... L, typename T, typename State>
  2059. struct reverse_find_<list<T, L...>, T, State>
  2060. : reverse_find_<list<L...>, T, list<T, L...>>
  2061. {
  2062. };
  2063. } // namespace detail
  2064. /// \endcond
  2065. /// Return the tail of the list \p L starting at the last occurrence of \p T, if any such
  2066. /// element exists; the empty list, otherwise.
  2067. /// \par Complexity
  2068. /// \f$ O(N) \f$.
  2069. /// \ingroup query
  2070. template <META_TYPE_CONSTRAINT(list_like) L, typename T>
  2071. using reverse_find = drop<L, min<reverse_find_index<L, T>, size<L>>>;
  2072. namespace lazy
  2073. {
  2074. /// \sa 'meta::rfind'
  2075. /// \ingroup lazy_query
  2076. template <typename L, typename T>
  2077. using reverse_find = defer<reverse_find, L, T>;
  2078. } // namespace lazy
  2079. ///////////////////////////////////////////////////////////////////////////////////////////
  2080. // find_if
  2081. /// \cond
  2082. namespace detail
  2083. {
  2084. #ifdef META_CONCEPT
  2085. template <typename L, typename Fn>
  2086. struct find_if_
  2087. {
  2088. };
  2089. template <typename Fn>
  2090. struct find_if_<list<>, Fn>
  2091. {
  2092. using type = list<>;
  2093. };
  2094. template <typename Head, typename... L, typename Fn>
  2095. requires integral<invoke<Fn, Head>>
  2096. struct find_if_<list<Head, L...>, Fn>
  2097. : if_<invoke<Fn, Head>, id<list<Head, L...>>, find_if_<list<L...>, Fn>>
  2098. {
  2099. };
  2100. #else
  2101. constexpr bool const *find_if_i_(bool const *const begin, bool const *const end)
  2102. {
  2103. return begin == end || *begin ? begin : find_if_i_(begin + 1, end);
  2104. }
  2105. template <typename L, typename Fn, typename = void>
  2106. struct find_if_
  2107. {
  2108. };
  2109. template <typename Fn>
  2110. struct find_if_<list<>, Fn>
  2111. {
  2112. using type = list<>;
  2113. };
  2114. template <typename... L, typename Fn>
  2115. struct find_if_<list<L...>, Fn,
  2116. void_<integer_sequence<bool, bool(invoke<Fn, L>::type::value)...>>>
  2117. {
  2118. #ifdef META_WORKAROUND_LLVM_28385
  2119. static constexpr bool s_v[sizeof...(L)] = {invoke<Fn, L>::type::value...};
  2120. #else
  2121. static constexpr bool s_v[] = {invoke<Fn, L>::type::value...};
  2122. #endif
  2123. using type =
  2124. drop_c<list<L...>, detail::find_if_i_(s_v, s_v + sizeof...(L)) - s_v>;
  2125. };
  2126. #endif
  2127. } // namespace detail
  2128. /// \endcond
  2129. /// Return the tail of the list \p L starting at the first element `A`
  2130. /// such that `invoke<Fn, A>::%value` is \c true, if any such element
  2131. /// exists; the empty list, otherwise.
  2132. /// \par Complexity
  2133. /// \f$ O(N) \f$.
  2134. /// \ingroup query
  2135. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2136. using find_if = _t<detail::find_if_<L, Fn>>;
  2137. namespace lazy
  2138. {
  2139. /// \sa 'meta::find_if'
  2140. /// \ingroup lazy_query
  2141. template <typename L, typename Fn>
  2142. using find_if = defer<find_if, L, Fn>;
  2143. } // namespace lazy
  2144. ////////////////////////////////////////////////////////////////////////////////////
  2145. // reverse_find_if
  2146. /// \cond
  2147. namespace detail
  2148. {
  2149. #ifdef META_CONCEPT
  2150. template <typename L, typename Fn, typename State = list<>>
  2151. struct reverse_find_if_
  2152. {
  2153. };
  2154. template <typename Fn, typename State>
  2155. struct reverse_find_if_<list<>, Fn, State>
  2156. {
  2157. using type = State;
  2158. };
  2159. template <typename Head, typename... L, typename Fn, typename State>
  2160. requires integral<invoke<Fn, Head>>
  2161. struct reverse_find_if_<list<Head, L...>, Fn, State>
  2162. : reverse_find_if_<list<L...>, Fn, if_<invoke<Fn, Head>, list<Head, L...>, State>>
  2163. {
  2164. };
  2165. #else
  2166. constexpr bool const *reverse_find_if_i_(bool const *const begin, bool const *const pos,
  2167. bool const *const end)
  2168. {
  2169. return begin == pos
  2170. ? end
  2171. : *(pos - 1) ? pos - 1 : reverse_find_if_i_(begin, pos - 1, end);
  2172. }
  2173. template <typename L, typename Fn, typename = void>
  2174. struct reverse_find_if_
  2175. {
  2176. };
  2177. template <typename Fn>
  2178. struct reverse_find_if_<list<>, Fn>
  2179. {
  2180. using type = list<>;
  2181. };
  2182. template <typename... L, typename Fn>
  2183. struct reverse_find_if_<
  2184. list<L...>, Fn,
  2185. void_<integer_sequence<bool, bool(invoke<Fn, L>::type::value)...>>>
  2186. {
  2187. #ifdef META_WORKAROUND_LLVM_28385
  2188. static constexpr bool s_v[sizeof...(L)] = {invoke<Fn, L>::type::value...};
  2189. #else
  2190. static constexpr bool s_v[] = {invoke<Fn, L>::type::value...};
  2191. #endif
  2192. using type =
  2193. drop_c<list<L...>, detail::reverse_find_if_i_(s_v, s_v + sizeof...(L),
  2194. s_v + sizeof...(L)) -
  2195. s_v>;
  2196. };
  2197. #endif
  2198. } // namespace detail
  2199. /// \endcond
  2200. /// Return the tail of the list \p L starting at the last element `A`
  2201. /// such that `invoke<Fn, A>::%value` is \c true, if any such element
  2202. /// exists; the empty list, otherwise.
  2203. /// \par Complexity
  2204. /// \f$ O(N) \f$.
  2205. /// \ingroup query
  2206. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2207. using reverse_find_if = _t<detail::reverse_find_if_<L, Fn>>;
  2208. namespace lazy
  2209. {
  2210. /// \sa 'meta::rfind_if'
  2211. /// \ingroup lazy_query
  2212. template <typename L, typename Fn>
  2213. using reverse_find_if = defer<reverse_find_if, L, Fn>;
  2214. } // namespace lazy
  2215. ///////////////////////////////////////////////////////////////////////////////////////////
  2216. // replace
  2217. /// \cond
  2218. namespace detail
  2219. {
  2220. template <typename L, typename T, typename U>
  2221. struct replace_
  2222. {
  2223. };
  2224. template <typename... L, typename T, typename U>
  2225. struct replace_<list<L...>, T, U>
  2226. {
  2227. using type = list<if_c<META_IS_SAME(T, L), U, L>...>;
  2228. };
  2229. } // namespace detail
  2230. /// \endcond
  2231. /// Return a new \c meta::list where all instances of type \p T have
  2232. /// been replaced with \p U.
  2233. /// \par Complexity
  2234. /// \f$ O(N) \f$.
  2235. /// \ingroup transformation
  2236. template <META_TYPE_CONSTRAINT(list_like) L, typename T, typename U>
  2237. using replace = _t<detail::replace_<L, T, U>>;
  2238. namespace lazy
  2239. {
  2240. /// \sa 'meta::replace'
  2241. /// \ingroup lazy_transformation
  2242. template <typename L, typename T, typename U>
  2243. using replace = defer<replace, T, U>;
  2244. } // namespace lazy
  2245. ///////////////////////////////////////////////////////////////////////////////////////////
  2246. // replace_if
  2247. /// \cond
  2248. namespace detail
  2249. {
  2250. #ifdef META_CONCEPT
  2251. template <typename L, typename C, typename U>
  2252. struct replace_if_
  2253. {
  2254. };
  2255. template <typename... L, typename C, typename U>
  2256. requires and_v<integral<invoke<C, L>>...>
  2257. struct replace_if_<list<L...>, C, U>
  2258. {
  2259. using type = list<if_<invoke<C, L>, U, L>...>;
  2260. };
  2261. #else
  2262. template <typename L, typename C, typename U, typename = void>
  2263. struct replace_if_
  2264. {
  2265. };
  2266. template <typename... L, typename C, typename U>
  2267. struct replace_if_<list<L...>, C, U,
  2268. void_<integer_sequence<bool, bool(invoke<C, L>::type::value)...>>>
  2269. {
  2270. using type = list<if_<invoke<C, L>, U, L>...>;
  2271. };
  2272. #endif
  2273. } // namespace detail
  2274. /// \endcond
  2275. /// Return a new \c meta::list where all elements \c A of the list \p L
  2276. /// for which `invoke<C,A>::%value` is \c true have been replaced with
  2277. /// \p U.
  2278. /// \par Complexity
  2279. /// \f$ O(N) \f$.
  2280. /// \ingroup transformation
  2281. template <META_TYPE_CONSTRAINT(list_like) L, typename C, typename U>
  2282. using replace_if = _t<detail::replace_if_<L, C, U>>;
  2283. namespace lazy
  2284. {
  2285. /// \sa 'meta::replace_if'
  2286. /// \ingroup lazy_transformation
  2287. template <typename L, typename C, typename U>
  2288. using replace_if = defer<replace_if, C, U>;
  2289. } // namespace lazy
  2290. ///////////////////////////////////////////////////////////////////////////////////////
  2291. // count
  2292. namespace detail
  2293. {
  2294. template <typename, typename>
  2295. struct count_
  2296. {
  2297. };
  2298. #if (defined(META_CONCEPT) || META_CXX_VARIABLE_TEMPLATES) && META_CXX_FOLD_EXPRESSIONS
  2299. template <typename... Ts, typename T>
  2300. struct count_<list<Ts...>, T>
  2301. {
  2302. using type = meta::size_t<((std::size_t)META_IS_SAME(T, Ts) + ...)>;
  2303. };
  2304. #else
  2305. constexpr std::size_t count_i_(bool const *const begin, bool const *const end,
  2306. std::size_t n)
  2307. {
  2308. return begin == end ? n : detail::count_i_(begin + 1, end, n + *begin);
  2309. }
  2310. template <typename T>
  2311. struct count_<list<>, T>
  2312. {
  2313. using type = meta::size_t<0>;
  2314. };
  2315. template <typename... L, typename T>
  2316. struct count_<list<L...>, T>
  2317. {
  2318. #ifdef META_WORKAROUND_LLVM_28385
  2319. static constexpr bool s_v[sizeof...(L)] = {META_IS_SAME(T, L)...};
  2320. #else
  2321. static constexpr bool s_v[] = {META_IS_SAME(T, L)...};
  2322. #endif
  2323. using type = meta::size_t<detail::count_i_(s_v, s_v + sizeof...(L), 0u)>;
  2324. };
  2325. #endif
  2326. } // namespace detail
  2327. /// Count the number of times a type \p T appears in the list \p L.
  2328. /// \par Complexity
  2329. /// \f$ O(N) \f$.
  2330. /// \ingroup query
  2331. template <META_TYPE_CONSTRAINT(list_like) L, typename T>
  2332. using count = _t<detail::count_<L, T>>;
  2333. namespace lazy
  2334. {
  2335. /// \sa `meta::count`
  2336. /// \ingroup lazy_query
  2337. template <typename L, typename T>
  2338. using count = defer<count, L, T>;
  2339. } // namespace lazy
  2340. ///////////////////////////////////////////////////////////////////////////////////////
  2341. // count_if
  2342. namespace detail
  2343. {
  2344. #if defined(META_CONCEPT) && META_CXX_FOLD_EXPRESSIONS
  2345. template <typename, typename>
  2346. struct count_if_
  2347. {
  2348. };
  2349. template <typename... Ts, typename Fn>
  2350. requires (integral<invoke<Fn, Ts>> && ...)
  2351. struct count_if_<list<Ts...>, Fn>
  2352. {
  2353. using type = meta::size_t<((std::size_t)(bool)_v<invoke<Fn, Ts>> + ...)>;
  2354. };
  2355. #else
  2356. template <typename L, typename Fn, typename = void>
  2357. struct count_if_
  2358. {
  2359. };
  2360. template <typename Fn>
  2361. struct count_if_<list<>, Fn>
  2362. {
  2363. using type = meta::size_t<0>;
  2364. };
  2365. template <typename... L, typename Fn>
  2366. struct count_if_<list<L...>, Fn,
  2367. void_<integer_sequence<bool, bool(invoke<Fn, L>::type::value)...>>>
  2368. {
  2369. #if META_CXX_FOLD_EXPRESSIONS
  2370. using type = meta::size_t<((std::size_t)(bool)invoke<Fn, L>::type::value + ...)>;
  2371. #else
  2372. #ifdef META_WORKAROUND_LLVM_28385
  2373. static constexpr bool s_v[sizeof...(L)] = {invoke<Fn, L>::type::value...};
  2374. #else
  2375. static constexpr bool s_v[] = {invoke<Fn, L>::type::value...};
  2376. #endif
  2377. using type = meta::size_t<detail::count_i_(s_v, s_v + sizeof...(L), 0u)>;
  2378. #endif // META_CXX_FOLD_EXPRESSIONS
  2379. };
  2380. #endif // META_CONCEPT
  2381. } // namespace detail
  2382. /// Count the number of times the predicate \p Fn evaluates to true for all the elements in
  2383. /// the list \p L.
  2384. /// \par Complexity
  2385. /// \f$ O(N) \f$.
  2386. /// \ingroup query
  2387. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2388. using count_if = _t<detail::count_if_<L, Fn>>;
  2389. namespace lazy
  2390. {
  2391. /// \sa `meta::count_if`
  2392. /// \ingroup lazy_query
  2393. template <typename L, typename Fn>
  2394. using count_if = defer<count_if, L, Fn>;
  2395. } // namespace lazy
  2396. ///////////////////////////////////////////////////////////////////////////////////////////
  2397. // filter
  2398. /// \cond
  2399. namespace detail
  2400. {
  2401. template <typename Pred>
  2402. struct filter_
  2403. {
  2404. template <typename A>
  2405. using invoke = if_c<invoke<Pred, A>::type::value, list<A>, list<>>;
  2406. };
  2407. } // namespace detail
  2408. /// \endcond
  2409. /// Returns a new meta::list where only those elements of \p L that satisfy the
  2410. /// Callable \p Pred such that `invoke<Pred,A>::%value` is \c true are present.
  2411. /// That is, those elements that don't satisfy the \p Pred are "removed".
  2412. /// \par Complexity
  2413. /// \f$ O(N) \f$.
  2414. /// \ingroup transformation
  2415. template <typename L, typename Pred>
  2416. using filter = join<transform<L, detail::filter_<Pred>>>;
  2417. namespace lazy
  2418. {
  2419. /// \sa 'meta::filter'
  2420. /// \ingroup lazy_transformation
  2421. template <typename L, typename Fn>
  2422. using filter = defer<filter, L, Fn>;
  2423. } // namespace lazy
  2424. ///////////////////////////////////////////////////////////////////////////////////////////
  2425. // static_const
  2426. ///\cond
  2427. namespace detail
  2428. {
  2429. template <typename T>
  2430. struct static_const
  2431. {
  2432. static constexpr T value{};
  2433. };
  2434. // Avoid potential ODR violations with global objects:
  2435. template <typename T>
  2436. constexpr T static_const<T>::value;
  2437. } // namespace detail
  2438. ///\endcond
  2439. ///////////////////////////////////////////////////////////////////////////////////////////
  2440. // for_each
  2441. /// \cond
  2442. namespace detail
  2443. {
  2444. struct for_each_fn
  2445. {
  2446. template <class Fn, class... Args>
  2447. constexpr auto operator()(list<Args...>, Fn f) const -> Fn
  2448. {
  2449. return (void)std::initializer_list<int>{((void)f(Args{}), 0)...}, f;
  2450. }
  2451. };
  2452. } // namespace detail
  2453. /// \endcond
  2454. #if META_CXX_INLINE_VARIABLES
  2455. /// `for_each(L, Fn)` calls the \p Fn for each
  2456. /// argument in the \p L.
  2457. /// \ingroup runtime
  2458. inline constexpr detail::for_each_fn for_each{};
  2459. #else
  2460. ///\cond
  2461. namespace
  2462. {
  2463. /// \endcond
  2464. /// `for_each(List, UnaryFunction)` calls the \p UnaryFunction for each
  2465. /// argument in the \p List.
  2466. /// \ingroup runtime
  2467. constexpr auto &&for_each = detail::static_const<detail::for_each_fn>::value;
  2468. /// \cond
  2469. }
  2470. /// \endcond
  2471. #endif
  2472. ///////////////////////////////////////////////////////////////////////////////////////////
  2473. // transpose
  2474. /// Given a list of lists of types \p ListOfLists, transpose the elements from the lists.
  2475. /// \par Complexity
  2476. /// \f$ O(N \times M) \f$, where \f$ N \f$ is the size of the outer list, and
  2477. /// \f$ M \f$ is the size of the inner lists.
  2478. /// \ingroup transformation
  2479. template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
  2480. using transpose = fold<ListOfLists, repeat_n<size<front<ListOfLists>>, list<>>,
  2481. bind_back<quote<transform>, quote<push_back>>>;
  2482. namespace lazy
  2483. {
  2484. /// \sa 'meta::transpose'
  2485. /// \ingroup lazy_transformation
  2486. template <typename ListOfLists>
  2487. using transpose = defer<transpose, ListOfLists>;
  2488. } // namespace lazy
  2489. ///////////////////////////////////////////////////////////////////////////////////////////
  2490. // zip_with
  2491. /// Given a list of lists of types \p ListOfLists and an invocable \p Fn, construct a new
  2492. /// list by calling \p Fn with the elements from the lists pairwise.
  2493. /// \par Complexity
  2494. /// \f$ O(N \times M) \f$, where \f$ N \f$ is the size of the outer list, and
  2495. /// \f$ M \f$ is the size of the inner lists.
  2496. /// \ingroup transformation
  2497. template <META_TYPE_CONSTRAINT(invocable) Fn, META_TYPE_CONSTRAINT(list_like) ListOfLists>
  2498. using zip_with = transform<transpose<ListOfLists>, uncurry<Fn>>;
  2499. namespace lazy
  2500. {
  2501. /// \sa 'meta::zip_with'
  2502. /// \ingroup lazy_transformation
  2503. template <typename Fn, typename ListOfLists>
  2504. using zip_with = defer<zip_with, Fn, ListOfLists>;
  2505. } // namespace lazy
  2506. ///////////////////////////////////////////////////////////////////////////////////////////
  2507. // zip
  2508. /// Given a list of lists of types \p ListOfLists, construct a new list by grouping the
  2509. /// elements from the lists pairwise into `meta::list`s.
  2510. /// \par Complexity
  2511. /// \f$ O(N \times M) \f$, where \f$ N \f$ is the size of the outer list, and \f$ M \f$
  2512. /// is the size of the inner lists.
  2513. /// \ingroup transformation
  2514. template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
  2515. using zip = transpose<ListOfLists>;
  2516. namespace lazy
  2517. {
  2518. /// \sa 'meta::zip'
  2519. /// \ingroup lazy_transformation
  2520. template <typename ListOfLists>
  2521. using zip = defer<zip, ListOfLists>;
  2522. } // namespace lazy
  2523. ///////////////////////////////////////////////////////////////////////////////////////////
  2524. // as_list
  2525. /// \cond
  2526. namespace detail
  2527. {
  2528. template <typename T>
  2529. using uncvref_t = _t<std::remove_cv<_t<std::remove_reference<T>>>>;
  2530. // Indirection here needed to avoid Core issue 1430
  2531. // https://wg21.link/cwg1430
  2532. template <typename Sequence>
  2533. struct as_list_ : lazy::invoke<uncurry<quote<list>>, Sequence>
  2534. {
  2535. };
  2536. } // namespace detail
  2537. /// \endcond
  2538. /// Turn a type into an instance of \c meta::list in a way determined by
  2539. /// \c meta::apply.
  2540. /// \ingroup list
  2541. template <typename Sequence>
  2542. using as_list = _t<detail::as_list_<detail::uncvref_t<Sequence>>>;
  2543. namespace lazy
  2544. {
  2545. /// \sa 'meta::as_list'
  2546. /// \ingroup lazy_list
  2547. template <typename Sequence>
  2548. using as_list = defer<as_list, Sequence>;
  2549. } // namespace lazy
  2550. ///////////////////////////////////////////////////////////////////////////////////////////
  2551. // reverse
  2552. /// \cond
  2553. namespace detail
  2554. {
  2555. template <typename L, typename State = list<>>
  2556. struct reverse_ : lazy::fold<L, State, quote<push_front>>
  2557. {
  2558. };
  2559. template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
  2560. typename T6, typename T7, typename T8, typename T9, typename... Ts,
  2561. typename... Us>
  2562. struct reverse_<list<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Ts...>, list<Us...>>
  2563. : reverse_<list<Ts...>, list<T9, T8, T7, T6, T5, T4, T3, T2, T1, T0, Us...>>
  2564. {
  2565. };
  2566. }
  2567. /// \endcond
  2568. /// Return a new \c meta::list by reversing the elements in the list \p L.
  2569. /// \par Complexity
  2570. /// \f$ O(N) \f$.
  2571. /// \ingroup transformation
  2572. template <META_TYPE_CONSTRAINT(list_like) L>
  2573. using reverse = _t<detail::reverse_<L>>;
  2574. namespace lazy
  2575. {
  2576. /// \sa 'meta::reverse'
  2577. /// \ingroup lazy_transformation
  2578. template <typename L>
  2579. using reverse = defer<reverse, L>;
  2580. } // namespace lazy
  2581. /// Logically negate the result of invocable \p Fn.
  2582. /// \ingroup trait
  2583. template <META_TYPE_CONSTRAINT(invocable) Fn>
  2584. using not_fn = compose<quote<not_>, Fn>;
  2585. namespace lazy
  2586. {
  2587. /// \sa 'meta::not_fn'
  2588. /// \ingroup lazy_trait
  2589. template <typename Fn>
  2590. using not_fn = defer<not_fn, Fn>;
  2591. } // namespace lazy
  2592. ///////////////////////////////////////////////////////////////////////////////////////////
  2593. // all_of
  2594. /// A Boolean integral constant wrapper around \c true if `invoke<Fn, A>::%value` is \c true
  2595. /// for all elements \c A in \c meta::list \p L; \c false, otherwise.
  2596. /// \par Complexity
  2597. /// \f$ O(N) \f$.
  2598. /// \ingroup query
  2599. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2600. using all_of = empty<find_if<L, not_fn<Fn>>>;
  2601. namespace lazy
  2602. {
  2603. /// \sa 'meta::all_of'
  2604. /// \ingroup lazy_query
  2605. template <typename L, typename Fn>
  2606. using all_of = defer<all_of, L, Fn>;
  2607. } // namespace lazy
  2608. ///////////////////////////////////////////////////////////////////////////////////////////
  2609. // any_of
  2610. /// A Boolean integral constant wrapper around \c true if `invoke<Fn, A>::%value` is
  2611. /// \c true for any element \c A in \c meta::list \p L; \c false, otherwise.
  2612. /// \par Complexity
  2613. /// \f$ O(N) \f$.
  2614. /// \ingroup query
  2615. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2616. using any_of = not_<empty<find_if<L, Fn>>>;
  2617. namespace lazy
  2618. {
  2619. /// \sa 'meta::any_of'
  2620. /// \ingroup lazy_query
  2621. template <typename L, typename Fn>
  2622. using any_of = defer<any_of, L, Fn>;
  2623. } // namespace lazy
  2624. ///////////////////////////////////////////////////////////////////////////////////////////
  2625. // none_of
  2626. /// A Boolean integral constant wrapper around \c true if `invoke<Fn, A>::%value` is
  2627. /// \c false for all elements \c A in \c meta::list \p L; \c false, otherwise.
  2628. /// \par Complexity
  2629. /// \f$ O(N) \f$.
  2630. /// \ingroup query
  2631. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2632. using none_of = empty<find_if<L, Fn>>;
  2633. namespace lazy
  2634. {
  2635. /// \sa 'meta::none_of'
  2636. /// \ingroup lazy_query
  2637. template <typename L, META_TYPE_CONSTRAINT(invocable) Fn>
  2638. using none_of = defer<none_of, L, Fn>;
  2639. } // namespace lazy
  2640. ///////////////////////////////////////////////////////////////////////////////////////////
  2641. // in
  2642. /// A Boolean integral constant wrapper around \c true if there is at least one occurrence
  2643. /// of \p T in \p L.
  2644. /// \par Complexity
  2645. /// \f$ O(N) \f$.
  2646. /// \ingroup query
  2647. template <META_TYPE_CONSTRAINT(list_like) L, typename T>
  2648. using in = not_<empty<find<L, T>>>;
  2649. namespace lazy
  2650. {
  2651. /// \sa 'meta::in'
  2652. /// \ingroup lazy_query
  2653. template <typename L, typename T>
  2654. using in = defer<in, L, T>;
  2655. } // namespace lazy
  2656. ///////////////////////////////////////////////////////////////////////////////////////////
  2657. // inherit
  2658. /// \cond
  2659. namespace detail
  2660. {
  2661. template <typename L>
  2662. struct inherit_
  2663. {
  2664. };
  2665. template <typename... L>
  2666. struct inherit_<list<L...>> : L...
  2667. {
  2668. using type = inherit_;
  2669. };
  2670. } // namespace detail
  2671. /// \endcond
  2672. /// A type that inherits from all the types in the list
  2673. /// \pre The types in the list must be unique
  2674. /// \pre All the types in the list must be non-final class types
  2675. /// \ingroup datatype
  2676. template <META_TYPE_CONSTRAINT(list_like) L>
  2677. using inherit = meta::_t<detail::inherit_<L>>;
  2678. namespace lazy
  2679. {
  2680. /// \sa 'meta::inherit'
  2681. /// \ingroup lazy_datatype
  2682. template <typename L>
  2683. using inherit = defer<inherit, L>;
  2684. } // namespace lazy
  2685. ///////////////////////////////////////////////////////////////////////////////////////////
  2686. // unique
  2687. /// \cond
  2688. namespace detail
  2689. {
  2690. template <typename Set, typename T>
  2691. struct in_
  2692. {
  2693. };
  2694. template <typename... Set, typename T>
  2695. struct in_<list<Set...>, T> : bool_<META_IS_BASE_OF(id<T>, inherit<list<id<Set>...>>)>
  2696. {
  2697. };
  2698. template <typename Set, typename T>
  2699. struct insert_back_
  2700. {
  2701. };
  2702. template <typename... Set, typename T>
  2703. struct insert_back_<list<Set...>, T>
  2704. {
  2705. using type = if_<in_<list<Set...>, T>, list<Set...>, list<Set..., T>>;
  2706. };
  2707. } // namespace detail
  2708. /// \endcond
  2709. /// Return a new \c meta::list where all duplicate elements have been removed.
  2710. /// \par Complexity
  2711. /// \f$ O(N^2) \f$.
  2712. /// \ingroup transformation
  2713. template <META_TYPE_CONSTRAINT(list_like) L>
  2714. using unique = fold<L, list<>, quote_trait<detail::insert_back_>>;
  2715. namespace lazy
  2716. {
  2717. /// \sa 'meta::unique'
  2718. /// \ingroup lazy_transformation
  2719. template <typename L>
  2720. using unique = defer<unique, L>;
  2721. } // namespace lazy
  2722. ///////////////////////////////////////////////////////////////////////////////////////////
  2723. // partition
  2724. /// \cond
  2725. namespace detail
  2726. {
  2727. template <typename Fn>
  2728. struct partition_
  2729. {
  2730. #ifdef META_CONCEPT
  2731. template <typename, typename>
  2732. #else
  2733. template <typename, typename, typename = void>
  2734. #endif
  2735. struct impl
  2736. {
  2737. };
  2738. template <typename... Yes, typename... No, typename A>
  2739. #ifdef META_CONCEPT
  2740. requires integral<invoke<Fn, A>>
  2741. struct impl<pair<list<Yes...>, list<No...>>, A>
  2742. #else
  2743. struct impl<pair<list<Yes...>, list<No...>>, A,
  2744. void_<bool_<invoke<Fn, A>::type::value>>>
  2745. #endif
  2746. {
  2747. using type = if_<invoke<Fn, A>, pair<list<Yes..., A>, list<No...>>,
  2748. pair<list<Yes...>, list<No..., A>>>;
  2749. };
  2750. template <typename State, typename A>
  2751. using invoke = _t<impl<State, A>>;
  2752. };
  2753. } // namespace detail
  2754. /// \endcond
  2755. /// Returns a pair of lists, where the elements of \p L that satisfy the
  2756. /// invocable \p Fn such that `invoke<Fn,A>::%value` is \c true are present in the
  2757. /// first list and the rest are in the second.
  2758. /// \par Complexity
  2759. /// \f$ O(N) \f$.
  2760. /// \ingroup transformation
  2761. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2762. using partition = fold<L, pair<list<>, list<>>, detail::partition_<Fn>>;
  2763. namespace lazy
  2764. {
  2765. /// \sa 'meta::partition'
  2766. /// \ingroup lazy_transformation
  2767. template <typename L, typename Fn>
  2768. using partition = defer<partition, L, Fn>;
  2769. } // namespace lazy
  2770. ///////////////////////////////////////////////////////////////////////////////////////////
  2771. // sort
  2772. /// \cond
  2773. namespace detail
  2774. {
  2775. template <META_TYPE_CONSTRAINT(invocable) Fn, typename A, typename B, typename... Ts>
  2776. using part_ = partition<list<B, Ts...>, bind_back<Fn, A>>;
  2777. #ifdef META_CONCEPT
  2778. template <list_like L, invocable Fn>
  2779. #else
  2780. template <typename, typename, typename = void>
  2781. #endif
  2782. struct sort_
  2783. {
  2784. };
  2785. template <typename Fn>
  2786. struct sort_<list<>, Fn>
  2787. {
  2788. using type = list<>;
  2789. };
  2790. template <typename A, typename Fn>
  2791. struct sort_<list<A>, Fn>
  2792. {
  2793. using type = list<A>;
  2794. };
  2795. template <typename A, typename B, typename... Ts, typename Fn>
  2796. #ifdef META_CONCEPT
  2797. requires trait<sort_<first<part_<Fn, A, B, Ts...>>, Fn>> &&
  2798. trait<sort_<second<part_<Fn, A, B, Ts...>>, Fn>>
  2799. struct sort_<list<A, B, Ts...>, Fn>
  2800. #else
  2801. struct sort_<
  2802. list<A, B, Ts...>, Fn,
  2803. void_<_t<sort_<first<part_<Fn, A, B, Ts...>>, Fn>>>>
  2804. #endif
  2805. {
  2806. using P = part_<Fn, A, B, Ts...>;
  2807. using type = concat<_t<sort_<first<P>, Fn>>, list<A>, _t<sort_<second<P>, Fn>>>;
  2808. };
  2809. } // namespace detail
  2810. /// \endcond
  2811. // clang-format off
  2812. /// Return a new \c meta::list that is sorted according to invocable predicate \p Fn.
  2813. /// \par Complexity
  2814. /// Expected: \f$ O(N log N) \f$
  2815. /// Worst case: \f$ O(N^2) \f$.
  2816. /// \code
  2817. /// using L0 = list<char[5], char[3], char[2], char[6], char[1], char[5], char[10]>;
  2818. /// using L1 = meta::sort<L0, lambda<_a, _b, lazy::less<lazy::sizeof_<_a>, lazy::sizeof_<_b>>>>;
  2819. /// static_assert(std::is_same_v<L1, list<char[1], char[2], char[3], char[5], char[5], char[6], char[10]>>, "");
  2820. /// \endcode
  2821. /// \ingroup transformation
  2822. // clang-format on
  2823. template <META_TYPE_CONSTRAINT(list_like) L, META_TYPE_CONSTRAINT(invocable) Fn>
  2824. using sort = _t<detail::sort_<L, Fn>>;
  2825. namespace lazy
  2826. {
  2827. /// \sa 'meta::sort'
  2828. /// \ingroup lazy_transformation
  2829. template <typename L, typename Fn>
  2830. using sort = defer<sort, L, Fn>;
  2831. } // namespace lazy
  2832. ////////////////////////////////////////////////////////////////////////////
  2833. // lambda_
  2834. /// \cond
  2835. namespace detail
  2836. {
  2837. template <typename T, int = 0>
  2838. struct protect_;
  2839. template <typename, int = 0>
  2840. struct vararg_;
  2841. template <typename T, int = 0>
  2842. struct is_valid_;
  2843. // Returns which branch to evaluate
  2844. template <typename If, typename... Ts>
  2845. #ifdef META_CONCEPT
  2846. using lazy_if_ = lazy::_t<defer<_if_, If, protect_<Ts>...>>;
  2847. #else
  2848. using lazy_if_ = lazy::_t<defer<_if_, list<If, protect_<Ts>...>>>;
  2849. #endif
  2850. template <typename A, typename T, typename Fn, typename Ts>
  2851. struct subst1_
  2852. {
  2853. using type = list<list<T>>;
  2854. };
  2855. template <typename T, typename Fn, typename Ts>
  2856. struct subst1_<Fn, T, Fn, Ts>
  2857. {
  2858. using type = list<>;
  2859. };
  2860. template <typename A, typename T, typename Fn, typename Ts>
  2861. struct subst1_<vararg_<A>, T, Fn, Ts>
  2862. {
  2863. using type = list<Ts>;
  2864. };
  2865. template <typename As, typename Ts>
  2866. using substitutions_ = push_back<
  2867. join<transform<
  2868. concat<As, repeat_n_c<size<Ts>{} + 2 - size<As>{}, back<As>>>,
  2869. concat<Ts, repeat_n_c<2, back<As>>>,
  2870. bind_back<quote_trait<subst1_>, back<As>, drop_c<Ts, size<As>{} - 2>>>>,
  2871. list<back<As>>>;
  2872. #if 0//def META_CONCEPT
  2873. template <list_like As, list_like Ts>
  2874. requires (_v<size<Ts>> + 2 >= _v<size<As>>)
  2875. using substitutions = substitutions_<As, Ts>;
  2876. #else // ^^^ concepts / no concepts vvv
  2877. template <typename As, typename Ts>
  2878. using substitutions =
  2879. #ifdef META_WORKAROUND_MSVC_702792
  2880. invoke<if_c<(size<Ts>::value + 2 >= size<As>::value), quote<substitutions_>>, As,
  2881. Ts>;
  2882. #else // ^^^ workaround ^^^ / vvv no workaround vvv
  2883. invoke<if_c<(size<Ts>{} + 2 >= size<As>{}), quote<substitutions_>>, As, Ts>;
  2884. #endif // META_WORKAROUND_MSVC_702792
  2885. #endif // META_CONCEPT
  2886. template <typename T>
  2887. struct is_vararg_ : std::false_type
  2888. {
  2889. };
  2890. template <typename T>
  2891. struct is_vararg_<vararg_<T>> : std::true_type
  2892. {
  2893. };
  2894. template <META_TYPE_CONSTRAINT(list_like) Tags>
  2895. using is_variadic_ = is_vararg_<at<push_front<Tags, void>, dec<size<Tags>>>>;
  2896. template <META_TYPE_CONSTRAINT(list_like) Tags, bool IsVariadic = is_variadic_<Tags>::value>
  2897. struct lambda_;
  2898. // Non-variadic lambda implementation
  2899. template <typename... As>
  2900. struct lambda_<list<As...>, false>
  2901. {
  2902. private:
  2903. static constexpr std::size_t arity = sizeof...(As) - 1;
  2904. using Tags = list<As...>; // Includes the lambda body as the last arg!
  2905. using Fn = back<Tags>;
  2906. template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
  2907. struct impl;
  2908. template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
  2909. using lazy_impl_ = lazy::_t<defer<impl, T, protect_<Args>>>;
  2910. #if 0//def META_CONCEPT
  2911. template <typename, list_like>
  2912. #else
  2913. template <typename, typename, typename = void>
  2914. #endif
  2915. struct subst_
  2916. {
  2917. };
  2918. template <template <typename...> class C, typename... Ts, typename Args>
  2919. #if 0//def META_CONCEPT
  2920. requires valid<C, _t<impl<Ts, Args>>...> struct subst_<defer<C, Ts...>, Args>
  2921. #else
  2922. struct subst_<defer<C, Ts...>, Args, void_<C<_t<impl<Ts, Args>>...>>>
  2923. #endif
  2924. {
  2925. using type = C<_t<impl<Ts, Args>>...>;
  2926. };
  2927. template <typename T, template <T...> class C, T... Is, typename Args>
  2928. #if 0//def META_CONCEPT
  2929. requires valid_i<T, C, Is...> struct subst_<defer_i<T, C, Is...>, Args>
  2930. #else
  2931. struct subst_<defer_i<T, C, Is...>, Args, void_<C<Is...>>>
  2932. #endif
  2933. {
  2934. using type = C<Is...>;
  2935. };
  2936. template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
  2937. struct impl : if_c<(reverse_find_index<Tags, T>() != npos()),
  2938. lazy::at<Args, reverse_find_index<Tags, T>>, id<T>>
  2939. {
  2940. };
  2941. template <typename T, typename Args>
  2942. struct impl<protect_<T>, Args>
  2943. {
  2944. using type = T;
  2945. };
  2946. template <typename T, typename Args>
  2947. struct impl<is_valid_<T>, Args>
  2948. {
  2949. using type = is_trait<impl<T, Args>>;
  2950. };
  2951. template <typename If, typename... Ts, typename Args>
  2952. struct impl<defer<if_, If, Ts...>, Args> // Short-circuit if_
  2953. : impl<lazy_impl_<lazy_if_<If, Ts...>, Args>, Args>
  2954. {
  2955. };
  2956. template <typename B, typename... Bs, typename Args>
  2957. struct impl<defer<and_, B, Bs...>, Args> // Short-circuit and_
  2958. : impl<lazy_impl_<lazy_if_<B, lazy::and_<Bs...>, protect_<std::false_type>>, Args>,
  2959. Args>
  2960. {
  2961. };
  2962. template <typename B, typename... Bs, typename Args>
  2963. struct impl<defer<or_, B, Bs...>, Args> // Short-circuit or_
  2964. : impl<lazy_impl_<lazy_if_<B, protect_<std::true_type>, lazy::or_<Bs...>>, Args>,
  2965. Args>
  2966. {
  2967. };
  2968. template <template <typename...> class C, typename... Ts, typename Args>
  2969. struct impl<defer<C, Ts...>, Args> : subst_<defer<C, Ts...>, Args>
  2970. {
  2971. };
  2972. template <typename T, template <T...> class C, T... Is, typename Args>
  2973. struct impl<defer_i<T, C, Is...>, Args> : subst_<defer_i<T, C, Is...>, Args>
  2974. {
  2975. };
  2976. template <template <typename...> class C, typename... Ts, typename Args>
  2977. struct impl<C<Ts...>, Args> : subst_<defer<C, Ts...>, Args>
  2978. {
  2979. };
  2980. template <typename... Ts, typename Args>
  2981. struct impl<lambda_<list<Ts...>, false>, Args>
  2982. {
  2983. using type = compose<uncurry<lambda_<list<As..., Ts...>, false>>,
  2984. curry<bind_front<quote<concat>, Args>>>;
  2985. };
  2986. template <typename... Bs, typename Args>
  2987. struct impl<lambda_<list<Bs...>, true>, Args>
  2988. {
  2989. using type = compose<typename lambda_<list<As..., Bs...>, true>::thunk,
  2990. bind_front<quote<concat>, transform<Args, quote<list>>>,
  2991. curry<bind_front<quote<substitutions>, list<Bs...>>>>;
  2992. };
  2993. public:
  2994. template <typename... Ts>
  2995. #ifdef META_CONCEPT
  2996. requires (sizeof...(Ts) == arity) using invoke = _t<impl<Fn, list<Ts..., Fn>>>;
  2997. #else
  2998. using invoke = _t<if_c<sizeof...(Ts) == arity, impl<Fn, list<Ts..., Fn>>>>;
  2999. #endif
  3000. };
  3001. // Lambda with variadic placeholder (broken out due to less efficient compile-time
  3002. // resource usage)
  3003. template <typename... As>
  3004. struct lambda_<list<As...>, true>
  3005. {
  3006. private:
  3007. template <META_TYPE_CONSTRAINT(list_like) T, bool IsVar>
  3008. friend struct lambda_;
  3009. using Tags = list<As...>; // Includes the lambda body as the last arg!
  3010. template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
  3011. struct impl;
  3012. template <META_TYPE_CONSTRAINT(list_like) Args>
  3013. using eval_impl_ = bind_back<quote_trait<impl>, Args>;
  3014. template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
  3015. using lazy_impl_ = lazy::_t<defer<impl, T, protect_<Args>>>;
  3016. template <template <typename...> class C, META_TYPE_CONSTRAINT(list_like) Args,
  3017. META_TYPE_CONSTRAINT(list_like) Ts>
  3018. using try_subst_ = apply<quote<C>, join<transform<Ts, eval_impl_<Args>>>>;
  3019. #if 0//def META_CONCEPT
  3020. template <typename, list_like>
  3021. #else
  3022. template <typename, typename, typename = void>
  3023. #endif
  3024. struct subst_
  3025. {
  3026. };
  3027. template <template <typename...> class C, typename... Ts, typename Args>
  3028. #if 0//def META_CONCEPT
  3029. requires is_true<try_subst_<C, Args, list<Ts...>>> struct subst_<defer<C, Ts...>, Args>
  3030. #else
  3031. struct subst_<defer<C, Ts...>, Args, void_<try_subst_<C, Args, list<Ts...>>>>
  3032. #endif
  3033. {
  3034. using type = list<try_subst_<C, Args, list<Ts...>>>;
  3035. };
  3036. template <typename T, template <T...> class C, T... Is, typename Args>
  3037. #if 0//def META_CONCEPT
  3038. requires valid_i<T, C, Is...> struct subst_<defer_i<T, C, Is...>, Args>
  3039. #else
  3040. struct subst_<defer_i<T, C, Is...>, Args, void_<C<Is...>>>
  3041. #endif
  3042. {
  3043. using type = list<C<Is...>>;
  3044. };
  3045. template <typename T, META_TYPE_CONSTRAINT(list_like) Args>
  3046. struct impl : if_c<(reverse_find_index<Tags, T>() != npos()),
  3047. lazy::at<Args, reverse_find_index<Tags, T>>, id<list<T>>>
  3048. {
  3049. };
  3050. template <typename T, typename Args>
  3051. struct impl<protect_<T>, Args>
  3052. {
  3053. using type = list<T>;
  3054. };
  3055. template <typename T, typename Args>
  3056. struct impl<is_valid_<T>, Args>
  3057. {
  3058. using type = list<is_trait<impl<T, Args>>>;
  3059. };
  3060. template <typename If, typename... Ts, typename Args>
  3061. struct impl<defer<if_, If, Ts...>, Args> // Short-circuit if_
  3062. : impl<lazy_impl_<lazy_if_<If, Ts...>, Args>, Args>
  3063. {
  3064. };
  3065. template <typename B, typename... Bs, typename Args>
  3066. struct impl<defer<and_, B, Bs...>, Args> // Short-circuit and_
  3067. : impl<lazy_impl_<lazy_if_<B, lazy::and_<Bs...>, protect_<std::false_type>>, Args>,
  3068. Args>
  3069. {
  3070. };
  3071. template <typename B, typename... Bs, typename Args>
  3072. struct impl<defer<or_, B, Bs...>, Args> // Short-circuit or_
  3073. : impl<lazy_impl_<lazy_if_<B, protect_<std::true_type>, lazy::or_<Bs...>>, Args>,
  3074. Args>
  3075. {
  3076. };
  3077. template <template <typename...> class C, typename... Ts, typename Args>
  3078. struct impl<defer<C, Ts...>, Args> : subst_<defer<C, Ts...>, Args>
  3079. {
  3080. };
  3081. template <typename T, template <T...> class C, T... Is, typename Args>
  3082. struct impl<defer_i<T, C, Is...>, Args> : subst_<defer_i<T, C, Is...>, Args>
  3083. {
  3084. };
  3085. template <template <typename...> class C, typename... Ts, typename Args>
  3086. struct impl<C<Ts...>, Args> : subst_<defer<C, Ts...>, Args>
  3087. {
  3088. };
  3089. template <typename... Bs, bool IsVar, typename Args>
  3090. struct impl<lambda_<list<Bs...>, IsVar>, Args>
  3091. {
  3092. using type =
  3093. list<compose<typename lambda_<list<As..., Bs...>, true>::thunk,
  3094. bind_front<quote<concat>, Args>,
  3095. curry<bind_front<quote<substitutions>, list<Bs...>>>>>;
  3096. };
  3097. struct thunk
  3098. {
  3099. template <typename S, typename R = _t<impl<back<Tags>, S>>>
  3100. #ifdef META_CONCEPT
  3101. requires (_v<size<R>> == 1) using invoke = front<R>;
  3102. #else
  3103. using invoke = if_c<size<R>{} == 1, front<R>>;
  3104. #endif
  3105. };
  3106. public:
  3107. template <typename... Ts>
  3108. using invoke = invoke<thunk, substitutions<Tags, list<Ts...>>>;
  3109. };
  3110. } // namespace detail
  3111. /// \endcond
  3112. ///////////////////////////////////////////////////////////////////////////////////////////
  3113. // lambda
  3114. /// For creating anonymous Invocables.
  3115. /// \code
  3116. /// using L = lambda<_a, _b, std::pair<_b, std::pair<_a, _a>>>;
  3117. /// using P = invoke<L, int, short>;
  3118. /// static_assert(std::is_same_v<P, std::pair<short, std::pair<int, int>>>, "");
  3119. /// \endcode
  3120. /// \ingroup trait
  3121. template <typename... Ts>
  3122. #ifdef META_CONCEPT
  3123. requires (sizeof...(Ts) > 0) using lambda = detail::lambda_<list<Ts...>>;
  3124. #else
  3125. using lambda = if_c<(sizeof...(Ts) > 0), detail::lambda_<list<Ts...>>>;
  3126. #endif
  3127. ///////////////////////////////////////////////////////////////////////////////////////////
  3128. // is_valid
  3129. /// For testing whether a deferred computation will succeed in a \c let or a \c lambda.
  3130. /// \ingroup trait
  3131. template <typename T>
  3132. using is_valid = detail::is_valid_<T>;
  3133. ///////////////////////////////////////////////////////////////////////////////////////////
  3134. // vararg
  3135. /// For defining variadic placeholders.
  3136. template <typename T>
  3137. using vararg = detail::vararg_<T>;
  3138. ///////////////////////////////////////////////////////////////////////////////////////////
  3139. // protect
  3140. /// For preventing the evaluation of a nested `defer`ed computation in a \c let or
  3141. /// \c lambda expression.
  3142. template <typename T>
  3143. using protect = detail::protect_<T>;
  3144. ///////////////////////////////////////////////////////////////////////////////////////////
  3145. // var
  3146. /// For use when defining local variables in \c meta::let expressions
  3147. /// \sa `meta::let`
  3148. template <typename Tag, typename Value>
  3149. struct var;
  3150. /// \cond
  3151. namespace detail
  3152. {
  3153. template <typename...>
  3154. struct let_
  3155. {
  3156. };
  3157. template <typename Fn>
  3158. struct let_<Fn>
  3159. {
  3160. using type = lazy::invoke<lambda<Fn>>;
  3161. };
  3162. template <typename Tag, typename Value, typename... Rest>
  3163. struct let_<var<Tag, Value>, Rest...>
  3164. {
  3165. using type = lazy::invoke<lambda<Tag, _t<let_<Rest...>>>, Value>;
  3166. };
  3167. } // namespace detail
  3168. /// \endcond
  3169. /// A lexically scoped expression with local variables.
  3170. ///
  3171. /// \code
  3172. /// template <typename T, typename L>
  3173. /// using find_index_ = let<
  3174. /// var<_a, L>,
  3175. /// var<_b, lazy::find<_a, T>>,
  3176. /// lazy::if_<
  3177. /// std::is_same<_b, list<>>,
  3178. /// meta::npos,
  3179. /// lazy::minus<lazy::size<_a>, lazy::size<_b>>>>;
  3180. /// static_assert(find_index_<int, list<short, int, float>>{} == 1, "");
  3181. /// static_assert(find_index_<double, list<short, int, float>>{} == meta::npos{}, "");
  3182. /// \endcode
  3183. /// \ingroup trait
  3184. template <typename... As>
  3185. using let = _t<_t<detail::let_<As...>>>;
  3186. namespace lazy
  3187. {
  3188. /// \sa `meta::let`
  3189. /// \ingroup lazy_trait
  3190. template <typename... As>
  3191. using let = defer<let, As...>;
  3192. } // namespace lazy
  3193. // Some argument placeholders for use in \c lambda expressions.
  3194. /// \ingroup trait
  3195. inline namespace placeholders
  3196. {
  3197. // regular placeholders:
  3198. struct _a;
  3199. struct _b;
  3200. struct _c;
  3201. struct _d;
  3202. struct _e;
  3203. struct _f;
  3204. struct _g;
  3205. struct _h;
  3206. struct _i;
  3207. // variadic placeholders:
  3208. using _args = vararg<void>;
  3209. using _args_a = vararg<_a>;
  3210. using _args_b = vararg<_b>;
  3211. using _args_c = vararg<_c>;
  3212. } // namespace placeholders
  3213. ///////////////////////////////////////////////////////////////////////////////////////////
  3214. // cartesian_product
  3215. /// \cond
  3216. namespace detail
  3217. {
  3218. template <typename M2, typename M>
  3219. struct cartesian_product_fn
  3220. {
  3221. template <typename X>
  3222. struct lambda0
  3223. {
  3224. template <typename Xs>
  3225. using lambda1 = list<push_front<Xs, X>>;
  3226. using type = join<transform<M2, quote<lambda1>>>;
  3227. };
  3228. using type = join<transform<M, quote_trait<lambda0>>>;
  3229. };
  3230. } // namespace detail
  3231. /// \endcond
  3232. /// Given a list of lists \p ListOfLists, return a new list of lists that is the Cartesian
  3233. /// Product. Like the `sequence` function from the Haskell Prelude.
  3234. /// \par Complexity
  3235. /// \f$ O(N \times M) \f$, where \f$ N \f$ is the size of the outer list, and
  3236. /// \f$ M \f$ is the size of the inner lists.
  3237. /// \ingroup transformation
  3238. template <META_TYPE_CONSTRAINT(list_like) ListOfLists>
  3239. using cartesian_product =
  3240. reverse_fold<ListOfLists, list<list<>>, quote_trait<detail::cartesian_product_fn>>;
  3241. namespace lazy
  3242. {
  3243. /// \sa 'meta::cartesian_product'
  3244. /// \ingroup lazy_transformation
  3245. template <typename ListOfLists>
  3246. using cartesian_product = defer<cartesian_product, ListOfLists>;
  3247. } // namespace lazy
  3248. /// \cond
  3249. ///////////////////////////////////////////////////////////////////////////////////////////
  3250. // add_const_if
  3251. namespace detail
  3252. {
  3253. template <bool>
  3254. struct add_const_if
  3255. {
  3256. template <typename T>
  3257. using invoke = T const;
  3258. };
  3259. template <>
  3260. struct add_const_if<false>
  3261. {
  3262. template <typename T>
  3263. using invoke = T;
  3264. };
  3265. } // namespace detail
  3266. template <bool If>
  3267. using add_const_if_c = detail::add_const_if<If>;
  3268. template <META_TYPE_CONSTRAINT(integral) If>
  3269. using add_const_if = add_const_if_c<If::type::value>;
  3270. /// \endcond
  3271. /// \cond
  3272. ///////////////////////////////////////////////////////////////////////////////////////////
  3273. // const_if
  3274. template <bool If, typename T>
  3275. using const_if_c = typename add_const_if_c<If>::template invoke<T>;
  3276. template <typename If, typename T>
  3277. using const_if = typename add_const_if<If>::template invoke<T>;
  3278. /// \endcond
  3279. /// \cond
  3280. namespace detail
  3281. {
  3282. template <typename State, typename Ch>
  3283. using atoi_ = if_c<(Ch::value >= '0' && Ch::value <= '9'),
  3284. std::integral_constant<typename State::value_type,
  3285. State::value * 10 + (Ch::value - '0')>>;
  3286. }
  3287. /// \endcond
  3288. inline namespace literals
  3289. {
  3290. /// A user-defined literal that generates objects of type \c meta::size_t.
  3291. /// \ingroup integral
  3292. template <char... Chs>
  3293. constexpr fold<list<char_<Chs>...>, meta::size_t<0>, quote<detail::atoi_>>
  3294. operator"" _z()
  3295. {
  3296. return {};
  3297. }
  3298. } // namespace literals
  3299. } // namespace meta
  3300. /// \cond
  3301. // Non-portable forward declarations of standard containers
  3302. #ifndef META_NO_STD_FORWARD_DECLARATIONS
  3303. #if defined(__apple_build_version__) || (defined(__clang__) && __clang_major__ < 6)
  3304. META_BEGIN_NAMESPACE_STD
  3305. META_BEGIN_NAMESPACE_VERSION
  3306. template <class>
  3307. class META_TEMPLATE_VIS allocator;
  3308. template <class, class>
  3309. struct META_TEMPLATE_VIS pair;
  3310. template <class>
  3311. struct META_TEMPLATE_VIS hash;
  3312. template <class>
  3313. struct META_TEMPLATE_VIS less;
  3314. template <class>
  3315. struct META_TEMPLATE_VIS equal_to;
  3316. template <class>
  3317. struct META_TEMPLATE_VIS char_traits;
  3318. #if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI
  3319. inline namespace __cxx11 {
  3320. #endif
  3321. template <class, class, class>
  3322. class META_TEMPLATE_VIS basic_string;
  3323. #if defined(_GLIBCXX_USE_CXX11_ABI) && _GLIBCXX_USE_CXX11_ABI
  3324. }
  3325. #endif
  3326. META_END_NAMESPACE_VERSION
  3327. META_BEGIN_NAMESPACE_CONTAINER
  3328. #if defined(__GLIBCXX__)
  3329. inline namespace __cxx11 {
  3330. #endif
  3331. template <class, class>
  3332. class META_TEMPLATE_VIS list;
  3333. #if defined(__GLIBCXX__)
  3334. }
  3335. #endif
  3336. template <class, class>
  3337. class META_TEMPLATE_VIS forward_list;
  3338. template <class, class>
  3339. class META_TEMPLATE_VIS vector;
  3340. template <class, class>
  3341. class META_TEMPLATE_VIS deque;
  3342. template <class, class, class, class>
  3343. class META_TEMPLATE_VIS map;
  3344. template <class, class, class, class>
  3345. class META_TEMPLATE_VIS multimap;
  3346. template <class, class, class>
  3347. class META_TEMPLATE_VIS set;
  3348. template <class, class, class>
  3349. class META_TEMPLATE_VIS multiset;
  3350. template <class, class, class, class, class>
  3351. class META_TEMPLATE_VIS unordered_map;
  3352. template <class, class, class, class, class>
  3353. class META_TEMPLATE_VIS unordered_multimap;
  3354. template <class, class, class, class>
  3355. class META_TEMPLATE_VIS unordered_set;
  3356. template <class, class, class, class>
  3357. class META_TEMPLATE_VIS unordered_multiset;
  3358. template <class, class>
  3359. class META_TEMPLATE_VIS queue;
  3360. template <class, class, class>
  3361. class META_TEMPLATE_VIS priority_queue;
  3362. template <class, class>
  3363. class META_TEMPLATE_VIS stack;
  3364. META_END_NAMESPACE_CONTAINER
  3365. META_END_NAMESPACE_STD
  3366. namespace meta
  3367. {
  3368. namespace detail
  3369. {
  3370. template <typename T, typename A = std::allocator<T>>
  3371. using std_list = std::list<T, A>;
  3372. template <typename T, typename A = std::allocator<T>>
  3373. using std_forward_list = std::forward_list<T, A>;
  3374. template <typename T, typename A = std::allocator<T>>
  3375. using std_vector = std::vector<T, A>;
  3376. template <typename T, typename A = std::allocator<T>>
  3377. using std_deque = std::deque<T, A>;
  3378. template <typename T, typename C = std::char_traits<T>, typename A = std::allocator<T>>
  3379. using std_basic_string = std::basic_string<T, C, A>;
  3380. template <typename K, typename V, typename C = std::less<K>,
  3381. typename A = std::allocator<std::pair<K const, V>>>
  3382. using std_map = std::map<K, V, C, A>;
  3383. template <typename K, typename V, typename C = std::less<K>,
  3384. typename A = std::allocator<std::pair<K const, V>>>
  3385. using std_multimap = std::multimap<K, V, C, A>;
  3386. template <typename K, typename C = std::less<K>, typename A = std::allocator<K>>
  3387. using std_set = std::set<K, C, A>;
  3388. template <typename K, typename C = std::less<K>, typename A = std::allocator<K>>
  3389. using std_multiset = std::multiset<K, C, A>;
  3390. template <typename K, typename V, typename H = std::hash<K>,
  3391. typename C = std::equal_to<K>,
  3392. typename A = std::allocator<std::pair<K const, V>>>
  3393. using std_unordered_map = std::unordered_map<K, V, H, C, A>;
  3394. template <typename K, typename V, typename H = std::hash<K>,
  3395. typename C = std::equal_to<K>,
  3396. typename A = std::allocator<std::pair<K const, V>>>
  3397. using std_unordered_multimap = std::unordered_multimap<K, V, H, C, A>;
  3398. template <typename K, typename H = std::hash<K>, typename C = std::equal_to<K>,
  3399. typename A = std::allocator<K>>
  3400. using std_unordered_set = std::unordered_set<K, H, C, A>;
  3401. template <typename K, typename H = std::hash<K>, typename C = std::equal_to<K>,
  3402. typename A = std::allocator<K>>
  3403. using std_unordered_multiset = std::unordered_multiset<K, H, C, A>;
  3404. template <typename T, typename C = std_deque<T>>
  3405. using std_queue = std::queue<T, C>;
  3406. template <typename T, typename C = std_vector<T>,
  3407. class D = std::less<typename C::value_type>>
  3408. using std_priority_queue = std::priority_queue<T, C, D>;
  3409. template <typename T, typename C = std_deque<T>>
  3410. using std_stack = std::stack<T, C>;
  3411. } // namespace detail
  3412. template <>
  3413. struct quote<::std::list> : quote<detail::std_list>
  3414. {
  3415. };
  3416. template <>
  3417. struct quote<::std::deque> : quote<detail::std_deque>
  3418. {
  3419. };
  3420. template <>
  3421. struct quote<::std::forward_list> : quote<detail::std_forward_list>
  3422. {
  3423. };
  3424. template <>
  3425. struct quote<::std::vector> : quote<detail::std_vector>
  3426. {
  3427. };
  3428. template <>
  3429. struct quote<::std::basic_string> : quote<detail::std_basic_string>
  3430. {
  3431. };
  3432. template <>
  3433. struct quote<::std::map> : quote<detail::std_map>
  3434. {
  3435. };
  3436. template <>
  3437. struct quote<::std::multimap> : quote<detail::std_multimap>
  3438. {
  3439. };
  3440. template <>
  3441. struct quote<::std::set> : quote<detail::std_set>
  3442. {
  3443. };
  3444. template <>
  3445. struct quote<::std::multiset> : quote<detail::std_multiset>
  3446. {
  3447. };
  3448. template <>
  3449. struct quote<::std::unordered_map> : quote<detail::std_unordered_map>
  3450. {
  3451. };
  3452. template <>
  3453. struct quote<::std::unordered_multimap> : quote<detail::std_unordered_multimap>
  3454. {
  3455. };
  3456. template <>
  3457. struct quote<::std::unordered_set> : quote<detail::std_unordered_set>
  3458. {
  3459. };
  3460. template <>
  3461. struct quote<::std::unordered_multiset> : quote<detail::std_unordered_multiset>
  3462. {
  3463. };
  3464. template <>
  3465. struct quote<::std::queue> : quote<detail::std_queue>
  3466. {
  3467. };
  3468. template <>
  3469. struct quote<::std::priority_queue> : quote<detail::std_priority_queue>
  3470. {
  3471. };
  3472. template <>
  3473. struct quote<::std::stack> : quote<detail::std_stack>
  3474. {
  3475. };
  3476. } // namespace meta
  3477. #endif
  3478. #endif
  3479. /// \endcond
  3480. #ifdef __clang__
  3481. #pragma GCC diagnostic pop
  3482. #endif
  3483. #endif