algorithm.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_YAP_ALGORITHM_HPP_INCLUDED
  7. #define BOOST_YAP_ALGORITHM_HPP_INCLUDED
  8. #include <boost/yap/algorithm_fwd.hpp>
  9. #include <boost/yap/user_macros.hpp>
  10. #include <boost/yap/detail/algorithm.hpp>
  11. #include <boost/hana/size.hpp>
  12. #include <boost/hana/comparing.hpp>
  13. namespace boost { namespace yap {
  14. #ifdef BOOST_NO_CONSTEXPR_IF
  15. namespace detail {
  16. template<typename Expr, bool MutableRvalueRef>
  17. struct deref_impl
  18. {
  19. decltype(auto) operator()(Expr && expr)
  20. {
  21. return std::move(*expr.elements[hana::llong_c<0>]);
  22. }
  23. };
  24. template<typename Expr>
  25. struct deref_impl<Expr, false>
  26. {
  27. decltype(auto) operator()(Expr && expr)
  28. {
  29. return *expr.elements[hana::llong_c<0>];
  30. }
  31. };
  32. }
  33. #endif
  34. /** "Dereferences" a reference-expression, forwarding its referent to
  35. the caller. */
  36. template<typename Expr>
  37. decltype(auto) deref(Expr && expr)
  38. {
  39. static_assert(
  40. is_expr<Expr>::value, "deref() is only defined for expressions.");
  41. static_assert(
  42. detail::remove_cv_ref_t<Expr>::kind == expr_kind::expr_ref,
  43. "deref() is only defined for expr_ref-kind expressions.");
  44. #ifdef BOOST_NO_CONSTEXPR_IF
  45. return detail::deref_impl < Expr,
  46. std::is_rvalue_reference<Expr>::value &&
  47. !std::is_const<std::remove_reference_t<Expr>>::value >
  48. {}(static_cast<Expr &&>(expr));
  49. #else
  50. using namespace hana::literals;
  51. if constexpr (
  52. std::is_rvalue_reference<Expr>::value &&
  53. !std::is_const<std::remove_reference_t<Expr>>::value) {
  54. return std::move(*expr.elements[0_c]);
  55. } else {
  56. return *expr.elements[0_c];
  57. }
  58. #endif
  59. }
  60. namespace detail {
  61. template<typename Tuple, long long I>
  62. struct lvalue_ref_ith_element
  63. : std::is_lvalue_reference<decltype(
  64. std::declval<Tuple>()[hana::llong<I>{}])>
  65. {
  66. };
  67. #ifdef BOOST_NO_CONSTEXPR_IF
  68. template<bool ValueOfTerminalsOnly, typename T>
  69. decltype(auto) value_impl(T && x);
  70. template<
  71. typename T,
  72. bool IsExprRef,
  73. bool ValueOfTerminalsOnly,
  74. bool TakeValue,
  75. bool IsLvalueRef>
  76. struct value_expr_impl;
  77. template<
  78. typename T,
  79. bool ValueOfTerminalsOnly,
  80. bool TakeValue,
  81. bool IsLvalueRef>
  82. struct value_expr_impl<
  83. T,
  84. true,
  85. ValueOfTerminalsOnly,
  86. TakeValue,
  87. IsLvalueRef>
  88. {
  89. decltype(auto) operator()(T && x)
  90. {
  91. return ::boost::yap::detail::value_impl<ValueOfTerminalsOnly>(
  92. ::boost::yap::deref(static_cast<T &&>(x)));
  93. }
  94. };
  95. template<typename T, bool ValueOfTerminalsOnly>
  96. struct value_expr_impl<T, false, ValueOfTerminalsOnly, true, true>
  97. {
  98. decltype(auto) operator()(T && x)
  99. {
  100. return x.elements[hana::llong_c<0>];
  101. }
  102. };
  103. template<typename T, bool ValueOfTerminalsOnly>
  104. struct value_expr_impl<T, false, ValueOfTerminalsOnly, true, false>
  105. {
  106. decltype(auto) operator()(T && x)
  107. {
  108. return std::move(x.elements[hana::llong_c<0>]);
  109. }
  110. };
  111. template<typename T, bool ValueOfTerminalsOnly, bool IsLvalueRef>
  112. struct value_expr_impl<
  113. T,
  114. false,
  115. ValueOfTerminalsOnly,
  116. false,
  117. IsLvalueRef>
  118. {
  119. decltype(auto) operator()(T && x) { return static_cast<T &&>(x); }
  120. };
  121. template<typename T, bool IsExpr, bool ValueOfTerminalsOnly>
  122. struct value_impl_t
  123. {
  124. decltype(auto) operator()(T && x)
  125. {
  126. constexpr expr_kind kind = detail::remove_cv_ref_t<T>::kind;
  127. constexpr detail::expr_arity arity = detail::arity_of<kind>();
  128. return value_expr_impl < T, kind == expr_kind::expr_ref,
  129. ValueOfTerminalsOnly,
  130. (ValueOfTerminalsOnly && kind == expr_kind::terminal) ||
  131. (!ValueOfTerminalsOnly &&
  132. arity == detail::expr_arity::one),
  133. std::is_lvalue_reference<T>::value ||
  134. detail::lvalue_ref_ith_element<
  135. decltype(x.elements),
  136. 0>::value > {}(static_cast<T &&>(x));
  137. }
  138. };
  139. template<typename T, bool ValueOfTerminalsOnly>
  140. struct value_impl_t<T, false, ValueOfTerminalsOnly>
  141. {
  142. decltype(auto) operator()(T && x) { return static_cast<T &&>(x); }
  143. };
  144. template<bool ValueOfTerminalsOnly, typename T>
  145. decltype(auto) value_impl(T && x)
  146. {
  147. return detail::
  148. value_impl_t<T, is_expr<T>::value, ValueOfTerminalsOnly>{}(
  149. static_cast<T &&>(x));
  150. }
  151. #else
  152. template<bool ValueOfTerminalsOnly, typename T>
  153. decltype(auto) value_impl(T && x)
  154. {
  155. if constexpr (is_expr<T>::value) {
  156. using namespace hana::literals;
  157. constexpr expr_kind kind = remove_cv_ref_t<T>::kind;
  158. constexpr expr_arity arity = arity_of<kind>();
  159. if constexpr (kind == expr_kind::expr_ref) {
  160. return value_impl<ValueOfTerminalsOnly>(
  161. ::boost::yap::deref(static_cast<T &&>(x)));
  162. } else if constexpr (
  163. kind == expr_kind::terminal ||
  164. (!ValueOfTerminalsOnly && arity == expr_arity::one)) {
  165. if constexpr (
  166. std::is_lvalue_reference<T>::value ||
  167. detail::
  168. lvalue_ref_ith_element<decltype(x.elements), 0>{}) {
  169. return x.elements[0_c];
  170. } else {
  171. return std::move(x.elements[0_c]);
  172. }
  173. } else {
  174. return static_cast<T &&>(x);
  175. }
  176. } else {
  177. return static_cast<T &&>(x);
  178. }
  179. }
  180. #endif
  181. }
  182. /** Forwards the sole element of \a x to the caller, possibly calling
  183. <code>deref()</code> first if \a x is a reference expression, or
  184. forwards \a x to the caller unchanged.
  185. More formally:
  186. - If \a x is not an expression, \a x is forwarded to the caller.
  187. - Otherwise, if \a x is a reference expression, the result is
  188. <code>value(deref(x))</code>.
  189. - Otherwise, if \a x is an expression with only one value (a unary
  190. expression or a terminal expression), the result is the forwarded
  191. first element of \a x.
  192. - Otherwise, \a x is forwarded to the caller. */
  193. template<typename T>
  194. decltype(auto) value(T && x)
  195. {
  196. return detail::value_impl<false>(static_cast<T &&>(x));
  197. }
  198. #ifdef BOOST_NO_CONSTEXPR_IF
  199. template<typename Expr, typename I>
  200. decltype(auto) get(Expr && expr, I const & i);
  201. namespace detail {
  202. template<long long I, typename Expr, bool IsExpr, bool IsLvalueRef>
  203. struct get_impl;
  204. template<long long I, typename Expr, bool IsLvalueRef>
  205. struct get_impl<I, Expr, true, IsLvalueRef>
  206. {
  207. decltype(auto) operator()(Expr && expr, hana::llong<I> i)
  208. {
  209. return ::boost::yap::get(
  210. ::boost::yap::deref(static_cast<Expr &&>(expr)), i);
  211. }
  212. };
  213. template<long long I, typename Expr>
  214. struct get_impl<I, Expr, false, true>
  215. {
  216. decltype(auto) operator()(Expr && expr, hana::llong<I> i)
  217. {
  218. return expr.elements[i];
  219. }
  220. };
  221. template<long long I, typename Expr>
  222. struct get_impl<I, Expr, false, false>
  223. {
  224. decltype(auto) operator()(Expr && expr, hana::llong<I> i)
  225. {
  226. return std::move(expr.elements[i]);
  227. }
  228. };
  229. }
  230. #endif
  231. /** Forwards the <i>i</i>-th element of \a expr to the caller. If \a
  232. expr is a reference expression, the result is <code>get(deref(expr),
  233. i)</code>.
  234. \note <code>get()</code> is only valid if \a Expr is an expression.
  235. */
  236. template<typename Expr, typename I>
  237. decltype(auto) get(Expr && expr, I const & i)
  238. {
  239. static_assert(
  240. is_expr<Expr>::value, "get() is only defined for expressions.");
  241. static_assert(
  242. hana::IntegralConstant<I>::value,
  243. "'i' must be an IntegralConstant");
  244. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  245. static_assert(
  246. kind == expr_kind::expr_ref ||
  247. (0 <= I::value &&
  248. I::value < decltype(hana::size(expr.elements))::value),
  249. "In get(expr, I), I must be a valid index into expr's tuple "
  250. "elements.");
  251. #ifdef BOOST_NO_CONSTEXPR_IF
  252. return detail::get_impl<
  253. I::value,
  254. Expr,
  255. kind == expr_kind::expr_ref,
  256. std::is_lvalue_reference<Expr>::value>{}(static_cast<Expr &&>(expr), i);
  257. #else
  258. using namespace hana::literals;
  259. if constexpr (kind == expr_kind::expr_ref) {
  260. return ::boost::yap::get(
  261. ::boost::yap::deref(static_cast<Expr &&>(expr)), i);
  262. } else {
  263. if constexpr (std::is_lvalue_reference<Expr>::value) {
  264. return expr.elements[i];
  265. } else {
  266. return std::move(expr.elements[i]);
  267. }
  268. }
  269. #endif
  270. }
  271. /** Returns <code>get(expr, boost::hana::llong_c<I>)</code>. */
  272. template<long long I, typename Expr>
  273. decltype(auto) get_c(Expr && expr)
  274. {
  275. return ::boost::yap::get(static_cast<Expr &&>(expr), hana::llong_c<I>);
  276. }
  277. /** Returns the left operand in a binary operator expression.
  278. Equivalent to <code>get(expr, 0_c)</code>.
  279. \note <code>left()</code> is only valid if \a Expr is a binary
  280. operator expression.
  281. */
  282. template<typename Expr>
  283. decltype(auto) left(Expr && expr)
  284. {
  285. using namespace hana::literals;
  286. return ::boost::yap::get(static_cast<Expr &&>(expr), 0_c);
  287. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  288. static_assert(
  289. kind == expr_kind::expr_ref ||
  290. detail::arity_of<kind>() == detail::expr_arity::two,
  291. "left() is only defined for binary expressions.");
  292. }
  293. /** Returns the right operand in a binary operator expression.
  294. Equivalent to <code>get(expr, 1_c)</code>.
  295. \note <code>right()</code> is only valid if \a Expr is a binary
  296. operator expression.
  297. */
  298. template<typename Expr>
  299. decltype(auto) right(Expr && expr)
  300. {
  301. using namespace hana::literals;
  302. return ::boost::yap::get(static_cast<Expr &&>(expr), 1_c);
  303. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  304. static_assert(
  305. kind == expr_kind::expr_ref ||
  306. detail::arity_of<kind>() == detail::expr_arity::two,
  307. "right() is only defined for binary expressions.");
  308. }
  309. /** Returns the condition expression in an if_else expression.
  310. Equivalent to <code>get(expr, 0_c)</code>.
  311. \note <code>cond()</code> is only valid if \a Expr is an
  312. <code>expr_kind::if_else</code> expression.
  313. */
  314. template<typename Expr>
  315. decltype(auto) cond(Expr && expr)
  316. {
  317. using namespace hana::literals;
  318. return ::boost::yap::get(static_cast<Expr &&>(expr), 0_c);
  319. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  320. static_assert(
  321. kind == expr_kind::expr_ref || kind == expr_kind::if_else,
  322. "cond() is only defined for if_else expressions.");
  323. }
  324. /** Returns the then-expression in an if_else expression.
  325. Equivalent to <code>get(expr, 1_c)</code>.
  326. \note <code>then()</code> is only valid if \a Expr is an
  327. <code>expr_kind::if_else</code> expression.
  328. */
  329. template<typename Expr>
  330. decltype(auto) then(Expr && expr)
  331. {
  332. using namespace hana::literals;
  333. return ::boost::yap::get(static_cast<Expr &&>(expr), 1_c);
  334. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  335. static_assert(
  336. kind == expr_kind::expr_ref || kind == expr_kind::if_else,
  337. "then() is only defined for if_else expressions.");
  338. }
  339. /** Returns the else-expression in an if_else expression.
  340. Equivalent to <code>get(expr, 2_c)</code>.
  341. \note <code>else_()</code> is only valid if \a Expr is an
  342. <code>expr_kind::if_else</code> expression.
  343. */
  344. template<typename Expr>
  345. decltype(auto) else_(Expr && expr)
  346. {
  347. using namespace hana::literals;
  348. return ::boost::yap::get(static_cast<Expr &&>(expr), 2_c);
  349. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  350. static_assert(
  351. kind == expr_kind::expr_ref || kind == expr_kind::if_else,
  352. "else_() is only defined for if_else expressions.");
  353. }
  354. /** Returns the callable in a call expression.
  355. Equivalent to <code>get(expr, 0)</code>.
  356. \note <code>callable()</code> is only valid if \a Expr is an
  357. <code>expr_kind::call</code> expression.
  358. */
  359. template<typename Expr>
  360. decltype(auto) callable(Expr && expr)
  361. {
  362. return ::boost::yap::get(static_cast<Expr &&>(expr), hana::llong_c<0>);
  363. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  364. static_assert(
  365. kind == expr_kind::expr_ref ||
  366. detail::arity_of<kind>() == detail::expr_arity::n,
  367. "callable() is only defined for call expressions.");
  368. }
  369. /** Returns the <i>i-th</i> argument expression in a call expression.
  370. Equivalent to <code>get(expr, i + 1)</code>.
  371. \note <code>argument()</code> is only valid if \a Expr is an
  372. <code>expr_kind::call</code> expression.
  373. */
  374. template<long long I, typename Expr>
  375. decltype(auto) argument(Expr && expr, hana::llong<I> i)
  376. {
  377. return ::boost::yap::get(
  378. static_cast<Expr &&>(expr), hana::llong_c<I + 1>);
  379. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  380. static_assert(
  381. kind == expr_kind::expr_ref ||
  382. detail::arity_of<kind>() == detail::expr_arity::n,
  383. "argument() is only defined for call expressions.");
  384. static_assert(
  385. kind == expr_kind::expr_ref ||
  386. (0 <= I && I < decltype(hana::size(expr.elements))::value - 1),
  387. "I must be a valid call-expression argument index.");
  388. }
  389. /** Makes a new expression instantiated from the expression template \a
  390. ExprTemplate, of kind \a Kind, with the given values as its
  391. elements.
  392. For each parameter P:
  393. - If P is an expression, P is moved into the result if P is an
  394. rvalue and captured by reference into the result otherwise.
  395. - Otherwise, P is wrapped in a terminal expression.
  396. \note <code>make_expression()</code> is only valid if the number of
  397. parameters passed is appropriate for \a Kind.
  398. */
  399. template<
  400. template<expr_kind, class> class ExprTemplate,
  401. expr_kind Kind,
  402. typename... T>
  403. auto make_expression(T &&... t)
  404. {
  405. constexpr detail::expr_arity arity = detail::arity_of<Kind>();
  406. static_assert(
  407. (arity == detail::expr_arity::one && sizeof...(T) == 1) ||
  408. (arity == detail::expr_arity::two && sizeof...(T) == 2) ||
  409. (arity == detail::expr_arity::three && sizeof...(T) == 3) ||
  410. arity == detail::expr_arity::n,
  411. "The number of parameters passed to make_expression() must "
  412. "match the arity "
  413. "implied by the expr_kind template parameter.");
  414. using tuple_type =
  415. hana::tuple<detail::operand_type_t<ExprTemplate, T>...>;
  416. return ExprTemplate<Kind, tuple_type>{tuple_type{
  417. detail::make_operand<detail::operand_type_t<ExprTemplate, T>>{}(
  418. static_cast<T &&>(t))...}};
  419. }
  420. /** Makes a new terminal expression instantiated from the expression
  421. template \a ExprTemplate, with the given value as its sole element.
  422. \note <code>make_terminal()</code> is only valid if \a T is \b not
  423. an expression.
  424. */
  425. template<template<expr_kind, class> class ExprTemplate, typename T>
  426. auto make_terminal(T && t)
  427. {
  428. static_assert(
  429. !is_expr<T>::value,
  430. "make_terminal() is only defined for non expressions.");
  431. using result_type = detail::operand_type_t<ExprTemplate, T>;
  432. using tuple_type = decltype(std::declval<result_type>().elements);
  433. return result_type{tuple_type{static_cast<T &&>(t)}};
  434. }
  435. #ifdef BOOST_NO_CONSTEXPR_IF
  436. namespace detail {
  437. template<
  438. template<expr_kind, class> class ExprTemplate,
  439. typename T,
  440. bool IsExpr>
  441. struct as_expr_impl
  442. {
  443. decltype(auto) operator()(T && t) { return static_cast<T &&>(t); }
  444. };
  445. template<template<expr_kind, class> class ExprTemplate, typename T>
  446. struct as_expr_impl<ExprTemplate, T, false>
  447. {
  448. decltype(auto) operator()(T && t)
  449. {
  450. return make_terminal<ExprTemplate>(static_cast<T &&>(t));
  451. }
  452. };
  453. }
  454. #endif
  455. /** Returns an expression formed from \a t as follows:
  456. - If \a t is an expression, \a t is forwarded to the caller.
  457. - Otherwise, \a t is wrapped in a terminal expression.
  458. */
  459. template<template<expr_kind, class> class ExprTemplate, typename T>
  460. decltype(auto) as_expr(T && t)
  461. {
  462. #ifdef BOOST_NO_CONSTEXPR_IF
  463. return detail::as_expr_impl<ExprTemplate, T, is_expr<T>::value>{}(
  464. static_cast<T &&>(t));
  465. #else
  466. if constexpr (is_expr<T>::value) {
  467. return static_cast<T &&>(t);
  468. } else {
  469. return make_terminal<ExprTemplate>(static_cast<T &&>(t));
  470. }
  471. #endif
  472. }
  473. /** A callable type that evaluates its contained expression when called.
  474. \see <code>make_expression_function()</code>
  475. */
  476. template<typename Expr>
  477. struct expression_function
  478. {
  479. template<typename... U>
  480. decltype(auto) operator()(U &&... u)
  481. {
  482. return ::boost::yap::evaluate(expr, static_cast<U &&>(u)...);
  483. }
  484. Expr expr;
  485. };
  486. namespace detail {
  487. template<expr_kind Kind, typename Tuple>
  488. struct expression_function_expr
  489. {
  490. static const expr_kind kind = Kind;
  491. Tuple elements;
  492. };
  493. }
  494. /** Returns a callable object that \a expr has been forwarded into. This
  495. is useful for using expressions as function objects.
  496. Lvalue expressions are stored in the result by reference; rvalue
  497. expressions are moved into the result.
  498. \note <code>make_expression_function()</code> is only valid if \a
  499. Expr is an expression.
  500. */
  501. template<typename Expr>
  502. auto make_expression_function(Expr && expr)
  503. {
  504. static_assert(
  505. is_expr<Expr>::value,
  506. "make_expression_function() is only defined for expressions.");
  507. using stored_type =
  508. detail::operand_type_t<detail::expression_function_expr, Expr &&>;
  509. return expression_function<stored_type>{
  510. detail::make_operand<stored_type>{}(static_cast<Expr &&>(expr))};
  511. }
  512. }}
  513. #include <boost/yap/detail/transform.hpp>
  514. namespace boost { namespace yap {
  515. /** Returns a transform object that replaces placeholders within an
  516. expression with the given values.
  517. */
  518. template<typename... T>
  519. auto replacements(T &&... t)
  520. {
  521. return detail::placeholder_transform_t<T...>(static_cast<T &&>(t)...);
  522. }
  523. /** Returns \a expr with the placeholders replaced by YAP terminals
  524. containing the given values.
  525. \note <code>replace_placeholders(expr, t...)</code> is only valid if
  526. \a expr is an expression, and <code>max_p <= sizeof...(t)</code>,
  527. where <code>max_p</code> is the maximum placeholder index in \a expr.
  528. */
  529. template<typename Expr, typename... T>
  530. decltype(auto) replace_placeholders(Expr && expr, T &&... t)
  531. {
  532. static_assert(
  533. is_expr<Expr>::value,
  534. "evaluate() is only defined for expressions.");
  535. return transform(
  536. static_cast<Expr &&>(expr), replacements(static_cast<T &&>(t)...));
  537. }
  538. /** Returns a transform object that evaluates an expression using the
  539. built-in semantics. The transform replaces any placeholders with the
  540. given values.
  541. */
  542. template<typename... T>
  543. inline auto evaluation(T &&... t)
  544. {
  545. return detail::evaluation_transform_t<T...>(static_cast<T &&>(t)...);
  546. }
  547. /** Evaluates \a expr using the built-in semantics, replacing any
  548. placeholders with the given values.
  549. \note <code>evaluate(expr)</code> is only valid if \a expr is an
  550. expression.
  551. */
  552. template<typename Expr, typename... T>
  553. decltype(auto) evaluate(Expr && expr, T &&... t)
  554. {
  555. static_assert(
  556. is_expr<Expr>::value,
  557. "evaluate() is only defined for expressions.");
  558. return transform(
  559. static_cast<Expr &&>(expr), evaluation(static_cast<T &&>(t)...));
  560. }
  561. namespace detail {
  562. template<typename... Transforms>
  563. constexpr auto make_transform_tuple(Transforms &... transforms)
  564. {
  565. return hana::tuple<Transforms *...>{&transforms...};
  566. }
  567. template<bool Strict>
  568. struct transform_
  569. {
  570. template<typename Expr, typename Transform, typename... Transforms>
  571. constexpr decltype(auto) operator()(
  572. Expr && expr, Transform & transform, Transforms &... transforms) const
  573. {
  574. auto transform_tuple =
  575. detail::make_transform_tuple(transform, transforms...);
  576. constexpr expr_kind kind = detail::remove_cv_ref_t<Expr>::kind;
  577. return detail::
  578. transform_impl<Strict, 0, kind == expr_kind::expr_ref>{}(
  579. static_cast<Expr &&>(expr), transform_tuple);
  580. }
  581. };
  582. }
  583. /** Returns the result of transforming (all or part of) \a expr using
  584. whatever overloads of <code>Transform::operator()</code> match \a
  585. expr.
  586. \note Transformations can do anything: they may have side effects;
  587. they may mutate values; they may mutate types; and they may do any
  588. combination of these.
  589. */
  590. template<typename Expr, typename Transform, typename... Transforms>
  591. constexpr decltype(auto)
  592. transform(Expr && expr, Transform && transform, Transforms &&... transforms)
  593. {
  594. static_assert(
  595. is_expr<Expr>::value,
  596. "transform() is only defined for expressions.");
  597. return detail::transform_<false>{}(
  598. static_cast<Expr &&>(expr), transform, transforms...);
  599. }
  600. /** Returns the result of transforming \a expr using whichever overload of
  601. <code>Transform::operator()</code> best matches \a expr. If no
  602. overload of <code>Transform::operator()</code> matches, a compile-time
  603. error results.
  604. \note Transformations can do anything: they may have side effects;
  605. they may mutate values; they may mutate types; and they may do any
  606. combination of these.
  607. */
  608. template<typename Expr, typename Transform, typename... Transforms>
  609. decltype(auto) transform_strict(
  610. Expr && expr, Transform && transform, Transforms &&... transforms)
  611. {
  612. static_assert(
  613. is_expr<Expr>::value,
  614. "transform() is only defined for expressions.");
  615. return detail::transform_<true>{}(
  616. static_cast<Expr &&>(expr), transform, transforms...);
  617. }
  618. }}
  619. #endif