call_if.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. #ifndef BOOST_CONTRACT_CALL_IF_HPP_
  2. #define BOOST_CONTRACT_CALL_IF_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. /** @file
  8. Statically disable compilation and execution of functor calls.
  9. @note These facilities allow to emulate C++17 <c>if constexpr</c> statements
  10. when used together with functor templates (and C++14 generic lambdas).
  11. Therefore, they are not useful on C++17 compilers where
  12. <c> if constexpr</c> can be directly used instead.
  13. */
  14. #include <boost/contract/detail/none.hpp>
  15. #include <boost/make_shared.hpp>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/utility/enable_if.hpp>
  18. #include <boost/config.hpp>
  19. /* PRIVATE */
  20. /** @cond */
  21. // Boost.ResultOf not always able to deduce lambda result type (on MSVC).
  22. #ifndef BOOST_NO_CXX11_DECLTYPE
  23. #include <boost/utility/declval.hpp>
  24. #define BOOST_CONTRACT_CALL_IF_RESULT_OF_(F) \
  25. decltype(boost::declval<F>()())
  26. #else
  27. #include <boost/utility/result_of.hpp>
  28. #define BOOST_CONTRACT_CALL_IF_RESULT_OF_(F) \
  29. typename boost::result_of<F()>::type
  30. #endif
  31. /** @endcond */
  32. /* CODE */
  33. namespace boost { namespace contract {
  34. /**
  35. Select compilation and execution of functor template calls using a static
  36. boolean predicate (not needed on C++17 compilers, use <c>if constexpr</c>
  37. instead).
  38. This class template has no members because it is never used directly, it is only
  39. used via its specializations.
  40. Usually this class template is instantiated only via the return value of
  41. @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
  42. @see @RefSect{extras.assertion_requirements__templates_,
  43. Assertion Requirements}
  44. @tparam Pred Static boolean predicate that selects which functor template
  45. call to compile and execute.
  46. @tparam Then Type of the functor template to call if the static predicate
  47. @c Pred is @c true.
  48. @tparam ThenResult Return type of then-branch functor template call (this is
  49. usually automatically deduced by this library so it is never explicitly
  50. specified by the user, and that is why it is often marked as
  51. @c internal_type in this documentation).
  52. */
  53. template<bool Pred, typename Then, typename ThenResult =
  54. #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
  55. boost::contract::detail::none
  56. #else
  57. internal_type
  58. #endif
  59. >
  60. struct call_if_statement {}; // Empty so cannot be used (but copyable).
  61. /**
  62. Template specialization to dispatch between then-branch functor template calls
  63. that return void and the ones that return non-void (not needed on C++17
  64. compilers, use <c>if constexpr</c> instead).
  65. The base class is a call-if statement so the else and else-if statements can be
  66. specified if needed.
  67. Usually this class template is instantiated only via the return value of
  68. @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
  69. @note The <c>result_of<Then()>::type</c> expression needs be evaluated only
  70. when the static predicate is already checked to be @c true (because
  71. @c Then() is required to compile only in that case).
  72. Thus, this template specialization introduces an extra level of
  73. indirection necessary for proper lazy evaluation of this result-of
  74. expression.
  75. @see @RefSect{extras.assertion_requirements__templates_,
  76. Assertion Requirements}
  77. @tparam Then Type of functor template to call when the static predicate is
  78. @c true (as it is for this template specialization).
  79. */
  80. template<typename Then>
  81. struct call_if_statement<true, Then,
  82. #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
  83. boost::contract::detail::none
  84. #else
  85. internal_type
  86. #endif
  87. > :
  88. call_if_statement<true, Then,
  89. #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
  90. BOOST_CONTRACT_CALL_IF_RESULT_OF_(Then)
  91. #else
  92. typename result_of<Then()>::type
  93. #endif
  94. >
  95. { // Copyable (as its base).
  96. /**
  97. Construct this object with the then-branch functor template.
  98. @param f Then-branch nullary functor template.
  99. The functor template call @c f() is compiled and called for this
  100. template specialization (because the if-statement static
  101. predicate is @c true).
  102. The return type of @c f() must be the same as (or implicitly
  103. convertible to) the return type of all other functor template
  104. calls specified for this call-if object.
  105. */
  106. explicit call_if_statement(Then f) : call_if_statement<true, Then,
  107. BOOST_CONTRACT_CALL_IF_RESULT_OF_(Then)>(f) {}
  108. };
  109. /**
  110. Template specialization to handle static predicates that are @c true for
  111. then-branch functor template calls that do not return void (not needed on C++17
  112. compilers, use <c>if constexpr</c> instead).
  113. Usually this class template is instantiated only via the return value of
  114. @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
  115. @see @RefSect{extras.assertion_requirements__templates_,
  116. Assertion Requirements}
  117. @tparam Then Type of functor template to call when the static predicate is
  118. @c true (as it is for this template specialization).
  119. @tparam ThenResult Non-void return type of the then-branch functor template
  120. call.
  121. */
  122. template<typename Then, typename ThenResult>
  123. struct call_if_statement<true, Then, ThenResult> { // Copyable (as *).
  124. /**
  125. Construct this object with the then-branch functor template.
  126. @param f Then-branch nullary functor template.
  127. The functor template call @c f() is actually compiled and
  128. executed for this template specialization (because the
  129. if-statement static predicate is @c true).
  130. The return type of @c f() must be the same as (or implicitly
  131. convertible to) the @p ThenResult type.
  132. */
  133. explicit call_if_statement(Then f) :
  134. r_(boost::make_shared<ThenResult>(f())) {}
  135. /**
  136. This implicit type conversion returns a copy of the value returned by the
  137. call to the then-branch functor template.
  138. */
  139. operator ThenResult() const { return *r_; }
  140. /**
  141. Specify the else-branch functor template.
  142. @param f Else-branch nullary functor template.
  143. The functor template call @c f() is never compiled or executed
  144. for this template specialization (because the if-statement
  145. static predicate is @c true).
  146. The return type of @c f() must be the same as (or implicitly
  147. convertible to) the @p ThenResult type.
  148. @return A copy of the value returned by the call to the then-branch functor
  149. template (because the else-branch functor template call is not
  150. executed).
  151. */
  152. template<typename Else>
  153. ThenResult else_(Else const&
  154. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  155. f
  156. #endif
  157. ) const { return *r_; }
  158. /**
  159. Specify an else-if-branch functor template (using a static boolean
  160. predicate).
  161. @param f Else-if-branch nullary functor template.
  162. The functor template call @c f() is never compiled or executed
  163. for this template specialization (because the if-statement
  164. static predicate is @c true).
  165. The return type of @c f() must be the same as (or implicitly
  166. convertible to) the @p ThenResult type.
  167. @tparam ElseIfPred Static boolean predicate selecting which functor
  168. template call to compile and execute.
  169. @return A call-if statement so the else statement and additional else-if
  170. statements can be specified if needed.
  171. Eventually, it will be the return value of the then-branch functor
  172. template call for this template specialization (because the
  173. if-statement static predicate is @c true).
  174. */
  175. template<bool ElseIfPred, typename ElseIfThen>
  176. call_if_statement<true, Then, ThenResult> else_if_c(
  177. ElseIfThen const&
  178. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
  179. f
  180. #endif
  181. ) const { return *this; }
  182. /**
  183. Specify an else-if-branch functor template (using a nullary boolean
  184. meta-function).
  185. @param f Else-if-branch nullary functor template.
  186. The functor template call @c f() is never compiled or executed
  187. for this template specialization (because the if-statement
  188. static predicate is @c true).
  189. The return type of @c f() must be the same as (or implicitly
  190. convertible to) the @p ThenResult type.
  191. @tparam ElseIfPred Nullary boolean meta-function selecting which functor
  192. template call to compile and execute.
  193. @return A call-if statement so the else statement and additional else-if
  194. statements can be specified if needed.
  195. Eventually, it will be the return value of the then-branch functor
  196. template call for this template specialization (because the
  197. if-statement static predicate is @c true).
  198. */
  199. template<class ElseIfPred, typename ElseIfThen>
  200. call_if_statement<true, Then, ThenResult> else_if(
  201. ElseIfThen const&
  202. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
  203. f
  204. #endif
  205. ) const { return *this; }
  206. private:
  207. boost::shared_ptr<ThenResult> r_;
  208. };
  209. /**
  210. Template specialization to handle static predicates that are @c true for
  211. then-branch functor template calls that return void (not needed on C++17
  212. compilers, use <c>if constexpr</c> instead).
  213. Usually this class template is instantiated only via the return value of
  214. @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
  215. @see @RefSect{extras.assertion_requirements__templates_,
  216. Assertion Requirements}
  217. @tparam Then Type of functor template to call when the static predicate if
  218. @c true (as it is for this template specialization).
  219. */
  220. template<typename Then>
  221. struct call_if_statement<true, Then, void> { // Copyable (no data).
  222. /**
  223. Construct this object with the then-branch functor template.
  224. @param f Then-branch nullary functor template.
  225. The functor template call @c f() is actually compiled and
  226. executed for this template specialization (because the
  227. if-statement static predicate is @c true).
  228. The return type of @c f() must be @c void for this template
  229. specialization (because the then-branch functor template calls
  230. return void).
  231. */
  232. explicit call_if_statement(Then f) { f(); }
  233. // Cannot provide `operator ThenResult()` here, because ThenResult is void.
  234. /**
  235. Specify the else-branch functor template.
  236. @param f Else-branch nullary functor template.
  237. The functor template call @c f() is never compiled or executed
  238. for this template specialization (because the if-statement
  239. static predicate is @c true).
  240. The return type of @c f() must be @c void for this template
  241. specialization (because the then-branch functor template calls
  242. return void).
  243. */
  244. template<typename Else>
  245. void else_(Else const&
  246. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  247. f
  248. #endif
  249. ) const {}
  250. /**
  251. Specify an else-if-branch functor template (using a static boolean
  252. predicate).
  253. @param f Else-if-branch nullary functor template.
  254. The functor template call @c f() is never compiled or executed
  255. for this template specialization (because the if-statement
  256. static predicate is @c true).
  257. The return type of @c f() must be @c void for this template
  258. specialization (because the then-branch functor template calls
  259. return void).
  260. @tparam ElseIfPred Static boolean predicate selecting which functor
  261. template call to compile and execute.
  262. @return A call-if statement so the else statement and additional else-if
  263. statements can be specified if needed.
  264. Eventually, it will return void for this template specialization
  265. (because the then-branch functor template calls return void).
  266. */
  267. template<bool ElseIfPred, typename ElseIfThen>
  268. call_if_statement<true, Then, void> else_if_c(
  269. ElseIfThen const&
  270. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
  271. f
  272. #endif
  273. ) const { return *this; }
  274. /**
  275. Specify an else-if-branch functor template (using a nullary boolean
  276. meta-function).
  277. @param f Else-if-branch nullary functor template.
  278. The functor template call @c f() is never compiled or executed
  279. for this template specialization (because the if-statement
  280. static predicate is @c true).
  281. The return type of @c f() must be @c void for this template
  282. specialization (because the then-branch functor template calls
  283. return void).
  284. @tparam ElseIfPred Nullary boolean meta-function selecting which functor
  285. template call to compile and execute.
  286. @return A call-if statement so the else statement and additional else-if
  287. statements can be specified if needed.
  288. Eventually, it will return void for this template specialization
  289. (because the then-branch functor template calls return void).
  290. */
  291. template<class ElseIfPred, typename ElseIfThen>
  292. call_if_statement<true, Then, void> else_if(
  293. ElseIfThen const&
  294. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN // Avoid unused param warning.
  295. f
  296. #endif
  297. ) const { return *this; }
  298. };
  299. /**
  300. Template specialization to handle static predicates that are @c false (not
  301. needed on C++17 compilers, use <c>if constexpr</c> instead).
  302. This template specialization handles all else-branch functor template calls
  303. (whether they return void or not).
  304. Usually this class template is instantiated only via the return value of
  305. @RefFunc{boost::contract::call_if} and @RefFunc{boost::contract::call_if_c}.
  306. @see @RefSect{extras.assertion_requirements__templates_,
  307. Assertion Requirements}
  308. @tparam Then Type of functor template to call when the static predicate is
  309. @c true (never the case for this template specialization).
  310. */
  311. template<typename Then> // Copyable (no data).
  312. struct call_if_statement<false, Then,
  313. #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
  314. boost::contract::detail::none
  315. #else
  316. internal_type
  317. #endif
  318. > {
  319. /**
  320. Construct this object with the then-branch functor template.
  321. @param f Then-branch nullary functor template.
  322. The functor template call @c f() is never compiled or executed
  323. for this template specialization (because the if-statement
  324. static predicate is @c false).
  325. The return type of @c f() must be the same as (or implicitly
  326. convertible to) the return type of all other functor template
  327. calls specified for this call-if object.
  328. */
  329. explicit call_if_statement(Then const&
  330. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  331. f
  332. #endif
  333. ) {}
  334. // Do not provide `operator result_type()` here, require else_ instead.
  335. /**
  336. Specify the else-branch functor template.
  337. @note The <c>result_of<Else()>::type</c> expression needs be evaluated
  338. only when the static predicate is already checked to be @c false
  339. (because @c Else() is required to compile only in that case).
  340. Thus, this result-of expression is evaluated lazily and only in
  341. instantiations of this template specialization.
  342. @param f Else-branch nullary functor template.
  343. The functor template call @c f() is actually compiled and
  344. executed for this template specialization (because the
  345. if-statement static predicate is @c false).
  346. The return type of @c f() must be the same as (or implicitly
  347. convertible to) the return type of all other functor template
  348. calls specified for this call-if object.
  349. @return A copy of the value returned by the call to the else-branch functor
  350. template @c f().
  351. */
  352. template<typename Else>
  353. #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
  354. BOOST_CONTRACT_CALL_IF_RESULT_OF_(Else)
  355. #else
  356. typename result_of<Else()>::type
  357. #endif
  358. else_(Else f) const { return f(); }
  359. /**
  360. Specify an else-if-branch functor template (using a static boolean
  361. predicate).
  362. @param f Else-if-branch nullary functor template.
  363. The functor template call @c f() is actually compiled and
  364. executed if and only if @c ElseIfPred is @c true (because the
  365. if-statement static predicate is already @c false for this
  366. template specialization).
  367. The return type of @c f() must be the same as (or implicitly
  368. convertible to) the return type of all other functor template
  369. calls specified for this call-if object.
  370. @tparam ElseIfPred Static boolean predicate selecting which functor
  371. template call to compile and execute.
  372. @return A call-if statement so the else statement and additional else-if
  373. statements can be specified if needed.
  374. Eventually, this will be the return value of the one functor
  375. template call being compiled and executed.
  376. */
  377. template<bool ElseIfPred, typename ElseIfThen>
  378. call_if_statement<ElseIfPred, ElseIfThen> else_if_c(ElseIfThen f) const {
  379. return call_if_statement<ElseIfPred, ElseIfThen>(f);
  380. }
  381. /**
  382. Specify an else-if-branch functor template (using a nullary boolen
  383. meta-function).
  384. @param f Else-if-branch nullary functor template.
  385. The functor template call @c f() is actually compiled and
  386. executed if and only if @c ElseIfPred::value is @c true (because
  387. the if-statement static predicate is already @c false for this
  388. template specialization).
  389. The return type of @c f() must be the same as (or implicitly
  390. convertible to) the return type of all other functor template
  391. calls specified for this call-if object.
  392. @tparam ElseIfPred Nullary boolean meta-function selecting which functor
  393. template call to compile and execute.
  394. @return A call-if statement so the else statement and additional else-if
  395. statements can be specified if needed.
  396. Eventually, this will be the return value of the one functor
  397. template call being compiled and executed.
  398. */
  399. template<class ElseIfPred, typename ElseIfThen>
  400. call_if_statement<ElseIfPred::value, ElseIfThen> else_if(ElseIfThen f)
  401. const {
  402. return call_if_statement<ElseIfPred::value, ElseIfThen>(f);
  403. }
  404. };
  405. /**
  406. Select compilation and execution of functor template calls using a static
  407. boolean predicate (not needed on C++17 compilers, use <c>if constexpr</c>
  408. instead).
  409. Create a call-if object with the specified then-branch functor template:
  410. @code
  411. boost::contract::call_if_c<Pred1>(
  412. then_functor_template1
  413. ).template else_if_c<Pred2>( // Optional.
  414. then_functor_template2
  415. ) // Optionally, other `else_if_c` or
  416. ... // `else_if`.
  417. .else_( // Optional for `void` functors,
  418. else_functor_template // but required for non `void`.
  419. )
  420. @endcode
  421. Optional functor templates for else-if-branches and the else-branch can be
  422. specified as needed (the else-branch function template is required if @c f
  423. returns non-void).
  424. @see @RefSect{extras.assertion_requirements__templates_,
  425. Assertion Requirements}
  426. @param f Then-branch nullary functor template.
  427. The functor template call @c f() is compiled and executed if and
  428. only if @c Pred is @c true.
  429. The return type of other functor template calls specified for this
  430. call-if statement (else-branch, else-if-branches, etc.) must be the
  431. same as (or implicitly convertible to) the return type of
  432. then-branch functor call @c f().
  433. @tparam Pred Static boolean predicate selecting which functor template call
  434. to compile and execute.
  435. @return A call-if statement so else and else-if statements can be specified if
  436. needed.
  437. Eventually, this will be the return value of the one functor template
  438. call being compiled and executed (which could also be @c void).
  439. */
  440. template<bool Pred, typename Then>
  441. call_if_statement<Pred, Then> call_if_c(Then f) {
  442. return call_if_statement<Pred, Then>(f);
  443. }
  444. /**
  445. Select compilation and execution of functor template calls using a nullary
  446. boolean meta-function (not needed on C++17 compilers, use <c>if constexpr</c>
  447. instead).
  448. This is equivalent to <c>boost::contract::call_if_c<Pred::value>(f)</c>.
  449. Create a call-if object with the specified then-branch functor template:
  450. @code
  451. boost::contract::call_if<Pred1>(
  452. then_functor_template1
  453. ).template else_if<Pred2>( // Optional.
  454. then_functor_template2
  455. ) // Optionally, other `else_if` or
  456. ... // `else_if_c`.
  457. .else_( // Optional for `void` functors,
  458. else_functor_template // but required for non `void`.
  459. )
  460. @endcode
  461. Optional functor templates for else-if-branches and the else-branch can be
  462. specified as needed (the else-branch functor template is required if @c f
  463. returns non-void).
  464. @see @RefSect{extras.assertion_requirements__templates_,
  465. Assertion Requirements}
  466. @param f Then-branch nullary functor template.
  467. The functor template call @c f() is compiled and executed if and
  468. only if @c Pred::value is @c true.
  469. The return type of other functor template calls specified for this
  470. call-if statement (else-branch, else-if-branches, etc.) must be the
  471. same as (or implicitly convertible to) the return type of
  472. then-branch functor template call @c f().
  473. @tparam Pred Nullary boolean meta-function selecting which functor template
  474. call to compile and execute.
  475. @return A call-if statement so else and else-if statements can be specified if
  476. needed.
  477. Eventually, this will be the return value of the one functor template
  478. call being compiled and executed (which could also be @c void).
  479. */
  480. template<class Pred, typename Then>
  481. call_if_statement<Pred::value, Then> call_if(Then f) {
  482. return call_if_statement<Pred::value, Then>(f);
  483. }
  484. /**
  485. Select compilation and execution of a boolean functor template condition using a
  486. static boolean predicate (not needed on C++17 compilers, use
  487. <c>if constexpr</c> instead).
  488. Compile and execute the nullary boolean functor template call @c f() if and only
  489. if the specified static boolean predicate @p Pred is @c true, otherwise
  490. trivially return @p else_ (@c true by default) at run-time.
  491. A call to <c>boost::contract::condition_if_c<Pred>(f, else_)</c> is logically
  492. equivalent to <c>boost::contract::call_if_c<Pred>(f, [] { return else_; })</c>
  493. (but its internal implementation is optimized and it does not actually use
  494. @c call_if_c).
  495. @see @RefSect{extras.assertion_requirements__templates_,
  496. Assertion Requirements}
  497. @param f Nullary boolean functor template.
  498. The functor template call @c f() is compiled and executed if and
  499. only if @c Pred is @c true.
  500. @tparam Pred Static boolean predicate selecting when the functor template
  501. call @c f() should be compiled and executed.
  502. @param else_ Boolean value to return when @c Pred is @c false (instead of
  503. compiling and executing the functor template call @c f()).
  504. @return Boolean value returned by @c f() if the static predicate @c Pred is
  505. @c true. Otherwise, trivially return @p else_.
  506. */
  507. #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
  508. template<bool Pred, typename Then>
  509. bool condition_if_c(Then f, bool else_ = true);
  510. #else
  511. // NOTE: condition_if is a very simple special case of call_if so it can be
  512. // trivially implemented using enable_if instead of call_if as done below.
  513. template<bool Pred, typename Then>
  514. typename boost::enable_if_c<Pred, bool>::type
  515. condition_if_c(Then f, bool /* else_ */ = true) { return f(); }
  516. template<bool Pred, typename Then>
  517. typename boost::disable_if_c<Pred, bool>::type
  518. condition_if_c(Then /* f */, bool else_ = true) { return else_; }
  519. #endif
  520. /**
  521. Select compilation and execution of a boolean functor template condition using a
  522. nullary boolean meta-function (not needed on C++17 compilers, use
  523. <c>if constexpr</c> instead).
  524. This is equivalent to
  525. <c>boost::contract::condition_if_c<Pred::value>(f, else_)</c>.
  526. Compile and execute the nullary boolean functor template call @c f() if and only
  527. if the specified nullary boolean meta-function @p Pred::value is @c true,
  528. otherwise trivially return @p else_ (@c true by default) at run-time.
  529. @see @RefSect{extras.assertion_requirements__templates_,
  530. Assertion Requirements}
  531. @param f Nullary boolean functor template.
  532. The functor template call @c f() is compiled and executed if and
  533. only if @c Pred::value is @c true.
  534. @param else_ Boolean value to return when @c Pred::value is @c false (instead
  535. of compiling and executing the functor template call @c f()).
  536. @tparam Pred Nullary boolean meta-function selecting when the functor
  537. template call @c f() should be compiled and executed.
  538. @return Boolean value returned by @c f() if the static predicate @c Pred::value
  539. is @c true. Otherwise, trivially return @p else_.
  540. */
  541. template<class Pred, typename Then>
  542. bool condition_if(Then f, bool else_ = true) {
  543. return condition_if_c<Pred::value>(f, else_);
  544. }
  545. } } // namespace
  546. #endif // #include guard