optional.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*!
  2. @file
  3. Forward declares `boost::hana::optional`.
  4. @copyright Louis Dionne 2013-2016
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_FWD_OPTIONAL_HPP
  9. #define BOOST_HANA_FWD_OPTIONAL_HPP
  10. #include <boost/hana/config.hpp>
  11. #include <boost/hana/detail/operators/adl.hpp>
  12. #include <boost/hana/fwd/core/make.hpp>
  13. BOOST_HANA_NAMESPACE_BEGIN
  14. //! @ingroup group-datatypes
  15. //! Optional value whose optional-ness is known at compile-time.
  16. //!
  17. //! An `optional` either contains a value (represented as `just(x)`), or
  18. //! it is empty (represented as `nothing`). In essence, `hana::optional`
  19. //! is pretty much like a `boost::optional` or the upcoming `std::optional`,
  20. //! except for the fact that whether a `hana::optional` is empty or not is
  21. //! known at compile-time. This can be particularly useful for returning
  22. //! from a function that might fail, but whose reason for failing is not
  23. //! important. Of course, whether the function will fail has to be known
  24. //! at compile-time.
  25. //!
  26. //! This is really an important difference between `hana::optional` and
  27. //! `std::optional`. Unlike `std::optional<T>{}` and `std::optional<T>{x}`
  28. //! who share the same type (`std::optional<T>`), `hana::just(x)` and
  29. //! `hana::nothing` do not share the same type, since the state of the
  30. //! optional has to be known at compile-time. Hence, whether a `hana::just`
  31. //! or a `hana::nothing` will be returned from a function has to be known
  32. //! at compile-time for the return type of that function to be computable
  33. //! by the compiler. This makes `hana::optional` well suited for static
  34. //! metaprogramming tasks, but very poor for anything dynamic.
  35. //!
  36. //!
  37. //! Interoperation with `type`s
  38. //! ---------------------------
  39. //! When a `just` contains an object of type `T` which is a `type`,
  40. //! it has a nested `::%type` alias equivalent to `T::%type`. `nothing`,
  41. //! however, never has a nested `::%type` alias. If `t` is a `type`,
  42. //! this allows `decltype(just(t))` to be seen as a nullary metafunction
  43. //! equivalent to `decltype(t)`. Along with the `sfinae` function,
  44. //! this allows `hana::optional` to interact seamlessly with
  45. //! SFINAE-friendly metafunctions.
  46. //! Example:
  47. //! @include example/optional/sfinae_friendly_metafunctions.cpp
  48. //!
  49. //!
  50. //! Modeled concepts
  51. //! ----------------
  52. //! 1. `Comparable`\n
  53. //! Two `optional`s are equal if and only if they are both empty or they
  54. //! both contain a value and those values are equal.
  55. //! @include example/optional/comparable.cpp
  56. //!
  57. //! 2. `Orderable`\n
  58. //! Optional values can be ordered by considering the value they are
  59. //! holding, if any. To handle the case of an empty optional value, we
  60. //! arbitrarily set `nothing` as being less than any other `just`. Hence,
  61. //! @code
  62. //! just(x) < just(y) if and only if x < y
  63. //! nothing < just(anything)
  64. //! @endcode
  65. //! Example:
  66. //! @include example/optional/orderable.cpp
  67. //!
  68. //! 3. `Functor`\n
  69. //! An optional value can be seen as a list containing either one element
  70. //! (`just(x)`) or no elements at all (`nothing`). As such, mapping
  71. //! a function over an optional value is equivalent to applying it to
  72. //! its value if there is one, and to `nothing` otherwise:
  73. //! @code
  74. //! transform(just(x), f) == just(f(x))
  75. //! transform(nothing, f) == nothing
  76. //! @endcode
  77. //! Example:
  78. //! @include example/optional/functor.cpp
  79. //!
  80. //! 4. `Applicative`\n
  81. //! First, a value can be made optional with `lift<optional_tag>`, which
  82. //! is equivalent to `just`. Second, one can feed an optional value to an
  83. //! optional function with `ap`, which will return `just(f(x))` if there
  84. //! is both a function _and_ a value, and `nothing` otherwise:
  85. //! @code
  86. //! ap(just(f), just(x)) == just(f(x))
  87. //! ap(nothing, just(x)) == nothing
  88. //! ap(just(f), nothing) == nothing
  89. //! ap(nothing, nothing) == nothing
  90. //! @endcode
  91. //! A simple example:
  92. //! @include example/optional/applicative.cpp
  93. //! A more complex example:
  94. //! @include example/optional/applicative.complex.cpp
  95. //!
  96. //! 5. `Monad`\n
  97. //! The `Monad` model makes it easy to compose actions that might fail.
  98. //! One can feed an optional value if there is one into a function with
  99. //! `chain`, which will return `nothing` if there is no value. Finally,
  100. //! optional-optional values can have their redundant level of optionality
  101. //! removed with `flatten`. Also note that the `|` operator can be used in
  102. //! place of the `chain` function.
  103. //! Example:
  104. //! @include example/optional/monad.cpp
  105. //!
  106. //! 6. `MonadPlus`\n
  107. //! The `MonadPlus` model allows choosing the first valid value out of
  108. //! two optional values with `concat`. If both optional values are
  109. //! `nothing`s, `concat` will return `nothing`.
  110. //! Example:
  111. //! @include example/optional/monad_plus.cpp
  112. //!
  113. //! 7. `Foldable`\n
  114. //! Folding an optional value is equivalent to folding a list containing
  115. //! either no elements (for `nothing`) or `x` (for `just(x)`).
  116. //! Example:
  117. //! @include example/optional/foldable.cpp
  118. //!
  119. //! 8. `Searchable`\n
  120. //! Searching an optional value is equivalent to searching a list
  121. //! containing `x` for `just(x)` and an empty list for `nothing`.
  122. //! Example:
  123. //! @include example/optional/searchable.cpp
  124. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  125. template <typename ...T>
  126. struct optional {
  127. // 5.3.1, Constructors
  128. //! Default-construct an `optional`. Only exists if the optional
  129. //! contains a value, and if that value is DefaultConstructible.
  130. constexpr optional() = default;
  131. //! Copy-construct an `optional`.
  132. //! An empty optional may only be copy-constructed from another
  133. //! empty `optional`, and an `optional` with a value may only be
  134. //! copy-constructed from another `optional` with a value.
  135. //! Furthermore, this constructor only exists if the value
  136. //! held in the `optional` is CopyConstructible.
  137. optional(optional const&) = default;
  138. //! Move-construct an `optional`.
  139. //! An empty optional may only be move-constructed from another
  140. //! empty `optional`, and an `optional` with a value may only be
  141. //! move-constructed from another `optional` with a value.
  142. //! Furthermore, this constructor only exists if the value
  143. //! held in the `optional` is MoveConstructible.
  144. optional(optional&&) = default;
  145. //! Construct an `optional` holding a value of type `T` from another
  146. //! object of type `T`. The value is copy-constructed.
  147. constexpr optional(T const& t)
  148. : value_(t)
  149. { }
  150. //! Construct an `optional` holding a value of type `T` from another
  151. //! object of type `T`. The value is move-constructed.
  152. constexpr optional(T&& t)
  153. : value_(static_cast<T&&>(t))
  154. { }
  155. // 5.3.3, Assignment
  156. //! Copy-assign an `optional`.
  157. //! An empty optional may only be copy-assigned from another empty
  158. //! `optional`, and an `optional` with a value may only be copy-assigned
  159. //! from another `optional` with a value. Furthermore, this assignment
  160. //! operator only exists if the value held in the `optional` is
  161. //! CopyAssignable.
  162. constexpr optional& operator=(optional const&) = default;
  163. //! Move-assign an `optional`.
  164. //! An empty optional may only be move-assigned from another empty
  165. //! `optional`, and an `optional` with a value may only be move-assigned
  166. //! from another `optional` with a value. Furthermore, this assignment
  167. //! operator only exists if the value held in the `optional` is
  168. //! MoveAssignable.
  169. constexpr optional& operator=(optional&&) = default;
  170. // 5.3.5, Observers
  171. //! Returns a pointer to the contained value, or a `nullptr` if the
  172. //! `optional` is empty.
  173. //!
  174. //!
  175. //! @note Overloads of this method are provided for both the `const`
  176. //! and the non-`const` cases.
  177. //!
  178. //!
  179. //! Example
  180. //! -------
  181. //! @include example/optional/value.cpp
  182. constexpr T* operator->();
  183. //! Extract the content of an `optional`, or fail at compile-time.
  184. //!
  185. //! If `*this` contains a value, that value is returned. Otherwise,
  186. //! a static assertion is triggered.
  187. //!
  188. //! @note
  189. //! Overloads of this method are provided for the cases where `*this`
  190. //! is a reference, a rvalue-reference and their `const` counterparts.
  191. //!
  192. //!
  193. //! Example
  194. //! -------
  195. //! @include example/optional/value.cpp
  196. constexpr T& value();
  197. //! Equivalent to `value()`, provided for convenience.
  198. //!
  199. //! @note
  200. //! Overloads of this method are provided for the cases where `*this`
  201. //! is a reference, a rvalue-reference and their `const` counterparts.
  202. //!
  203. //!
  204. //! Example
  205. //! -------
  206. //! @include example/optional/value.cpp
  207. constexpr T& operator*();
  208. //! Return the contents of an `optional`, with a fallback result.
  209. //!
  210. //! If `*this` contains a value, that value is returned. Otherwise,
  211. //! the default value provided is returned.
  212. //!
  213. //! @note
  214. //! Overloads of this method are provided for the cases where `*this`
  215. //! is a reference, a rvalue-reference and their `const` counterparts.
  216. //!
  217. //!
  218. //! @param default_
  219. //! The default value to return if `*this` does not contain a value.
  220. //!
  221. //!
  222. //! Example
  223. //! -------
  224. //! @include example/optional/value_or.cpp
  225. template <typename U>
  226. constexpr decltype(auto) value_or(U&& default_);
  227. //! Equivalent to `hana::chain`.
  228. template <typename ...T, typename F>
  229. friend constexpr auto operator|(optional<T...>, F);
  230. //! Equivalent to `hana::equal`
  231. template <typename X, typename Y>
  232. friend constexpr auto operator==(X&& x, Y&& y);
  233. //! Equivalent to `hana::not_equal`
  234. template <typename X, typename Y>
  235. friend constexpr auto operator!=(X&& x, Y&& y);
  236. //! Equivalent to `hana::less`
  237. template <typename X, typename Y>
  238. friend constexpr auto operator<(X&& x, Y&& y);
  239. //! Equivalent to `hana::greater`
  240. template <typename X, typename Y>
  241. friend constexpr auto operator>(X&& x, Y&& y);
  242. //! Equivalent to `hana::less_equal`
  243. template <typename X, typename Y>
  244. friend constexpr auto operator<=(X&& x, Y&& y);
  245. //! Equivalent to `hana::greater_equal`
  246. template <typename X, typename Y>
  247. friend constexpr auto operator>=(X&& x, Y&& y);
  248. };
  249. #else
  250. template <typename ...T>
  251. struct optional;
  252. #endif
  253. //! Tag representing a `hana::optional`.
  254. //! @relates hana::optional
  255. struct optional_tag { };
  256. //! Create an optional value.
  257. //! @relates hana::optional
  258. //!
  259. //! Specifically, `make<optional_tag>()` is equivalent to `nothing`, and
  260. //! `make<optional_tag>(x)` is equivalent to `just(x)`. This is provided
  261. //! for consistency with the other `make<...>` functions.
  262. //!
  263. //!
  264. //! Example
  265. //! -------
  266. //! @include example/optional/make.cpp
  267. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  268. template <>
  269. constexpr auto make<optional_tag> = []([auto&& x]) {
  270. return optional<std::decay<decltype(x)>::type>{forwarded(x)};
  271. };
  272. #endif
  273. //! Alias to `make<optional_tag>`; provided for convenience.
  274. //! @relates hana::optional
  275. //!
  276. //!
  277. //! Example
  278. //! -------
  279. //! @include example/optional/make.cpp
  280. constexpr auto make_optional = make<optional_tag>;
  281. //! Create an optional value containing `x`.
  282. //! @relates hana::optional
  283. //!
  284. //!
  285. //! Example
  286. //! -------
  287. //! @include example/optional/just.cpp
  288. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  289. constexpr auto just = [](auto&& x) {
  290. return optional<std::decay<decltype(x)>::type>{forwarded(x)};
  291. };
  292. #else
  293. struct make_just_t {
  294. template <typename T>
  295. constexpr auto operator()(T&&) const;
  296. };
  297. constexpr make_just_t just{};
  298. #endif
  299. //! An empty optional value.
  300. //! @relates hana::optional
  301. //!
  302. //!
  303. //! Example
  304. //! -------
  305. //! @include example/optional/nothing.cpp
  306. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  307. constexpr optional<> nothing{};
  308. #else
  309. template <>
  310. struct optional<> : detail::operators::adl<optional<>> {
  311. // 5.3.1, Constructors
  312. constexpr optional() = default;
  313. constexpr optional(optional const&) = default;
  314. constexpr optional(optional&&) = default;
  315. // 5.3.3, Assignment
  316. constexpr optional& operator=(optional const&) = default;
  317. constexpr optional& operator=(optional&&) = default;
  318. // 5.3.5, Observers
  319. constexpr decltype(nullptr) operator->() const { return nullptr; }
  320. template <typename ...dummy>
  321. constexpr auto value() const;
  322. template <typename ...dummy>
  323. constexpr auto operator*() const;
  324. template <typename U>
  325. constexpr U&& value_or(U&& u) const;
  326. };
  327. constexpr optional<> nothing{};
  328. #endif
  329. //! Apply a function to the contents of an optional, with a fallback
  330. //! result.
  331. //! @relates hana::optional
  332. //!
  333. //! Specifically, `maybe` takes a default value, a function and an
  334. //! optional value. If the optional value is `nothing`, the default
  335. //! value is returned. Otherwise, the function is applied to the
  336. //! content of the `just`.
  337. //!
  338. //!
  339. //! @param default_
  340. //! A default value returned if `m` is `nothing`.
  341. //!
  342. //! @param f
  343. //! A function called as `f(x)` if and only if `m` is an optional value
  344. //! of the form `just(x)`. In that case, the result returend by `maybe`
  345. //! is the result of `f`.
  346. //!
  347. //! @param m
  348. //! An optional value.
  349. //!
  350. //!
  351. //! Example
  352. //! -------
  353. //! @include example/optional/maybe.cpp
  354. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  355. constexpr auto maybe = [](auto&& default_, auto&& f, auto&& m) -> decltype(auto) {
  356. if (m is a just(x)) {
  357. return forwarded(f)(forwarded(x));
  358. else
  359. return forwarded(default_);
  360. }
  361. };
  362. #else
  363. struct maybe_t {
  364. template <typename Def, typename F, typename T>
  365. constexpr decltype(auto) operator()(Def&&, F&& f, optional<T> const& m) const
  366. { return static_cast<F&&>(f)(m.value_); }
  367. template <typename Def, typename F, typename T>
  368. constexpr decltype(auto) operator()(Def&&, F&& f, optional<T>& m) const
  369. { return static_cast<F&&>(f)(m.value_); }
  370. template <typename Def, typename F, typename T>
  371. constexpr decltype(auto) operator()(Def&&, F&& f, optional<T>&& m) const
  372. { return static_cast<F&&>(f)(static_cast<optional<T>&&>(m).value_); }
  373. template <typename Def, typename F>
  374. constexpr Def operator()(Def&& def, F&&, optional<> const&) const
  375. { return static_cast<Def&&>(def); }
  376. };
  377. constexpr maybe_t maybe{};
  378. #endif
  379. //! Calls a function if the call expression is well-formed.
  380. //! @relates hana::optional
  381. //!
  382. //! Given a function `f`, `sfinae` returns a new function applying `f`
  383. //! to its arguments and returning `just` the result if the call is
  384. //! well-formed, and `nothing` otherwise. In other words, `sfinae(f)(x...)`
  385. //! is `just(f(x...))` if that expression is well-formed, and `nothing`
  386. //! otherwise. Note, however, that it is possible for an expression
  387. //! `f(x...)` to be well-formed as far as SFINAE is concerned, but
  388. //! trying to actually compile `f(x...)` still fails. In this case,
  389. //! `sfinae` won't be able to detect it and a hard failure is likely
  390. //! to happen.
  391. //!
  392. //!
  393. //! @note
  394. //! The function given to `sfinae` must not return `void`, since
  395. //! `just(void)` does not make sense. A compilation error is
  396. //! triggered if the function returns void.
  397. //!
  398. //!
  399. //! Example
  400. //! -------
  401. //! @include example/optional/sfinae.cpp
  402. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  403. auto sfinae = [](auto&& f) {
  404. return [perfect-capture](auto&& ...x) {
  405. if (decltype(forwarded(f)(forwarded(x)...)) is well-formed)
  406. return just(forwarded(f)(forwarded(x)...));
  407. else
  408. return nothing;
  409. };
  410. };
  411. #else
  412. struct sfinae_t {
  413. template <typename F>
  414. constexpr decltype(auto) operator()(F&& f) const;
  415. };
  416. constexpr sfinae_t sfinae{};
  417. #endif
  418. //! Return whether an `optional` contains a value.
  419. //! @relates hana::optional
  420. //!
  421. //! Specifically, returns a compile-time true-valued `Logical` if `m` is
  422. //! of the form `just(x)` for some `x`, and a false-valued one otherwise.
  423. //!
  424. //!
  425. //! Example
  426. //! -------
  427. //! @include example/optional/is_just.cpp
  428. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  429. constexpr auto is_just = [](auto const& m) {
  430. return m is a just(x);
  431. };
  432. #else
  433. struct is_just_t {
  434. template <typename ...T>
  435. constexpr auto operator()(optional<T...> const&) const;
  436. };
  437. constexpr is_just_t is_just{};
  438. #endif
  439. //! Return whether an `optional` is empty.
  440. //! @relates hana::optional
  441. //!
  442. //! Specifically, returns a compile-time true-valued `Logical` if `m` is
  443. //! a `nothing`, and a false-valued one otherwise.
  444. //!
  445. //!
  446. //! Example
  447. //! -------
  448. //! @include example/optional/is_nothing.cpp
  449. #ifdef BOOST_HANA_DOXYGEN_INVOKED
  450. constexpr auto is_nothing = [](auto const& m) {
  451. return m is a nothing;
  452. };
  453. #else
  454. struct is_nothing_t {
  455. template <typename ...T>
  456. constexpr auto operator()(optional<T...> const&) const;
  457. };
  458. constexpr is_nothing_t is_nothing{};
  459. #endif
  460. BOOST_HANA_NAMESPACE_END
  461. #endif // !BOOST_HANA_FWD_OPTIONAL_HPP