basic_result.hpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* A very simple result type
  2. (C) 2017-2020 Niall Douglas <http://www.nedproductions.biz/> (14 commits)
  3. File Created: June 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_BASIC_RESULT_HPP
  26. #define BOOST_OUTCOME_BASIC_RESULT_HPP
  27. #include "config.hpp"
  28. #include "convert.hpp"
  29. #include "detail/basic_result_final.hpp"
  30. #include "policy/all_narrow.hpp"
  31. #include "policy/terminate.hpp"
  32. #ifdef __clang__
  33. #pragma clang diagnostic push
  34. #pragma clang diagnostic ignored "-Wdocumentation" // Standardese markup confuses clang
  35. #endif
  36. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  37. template <class R, class S, class NoValuePolicy> //
  38. class basic_result;
  39. namespace detail
  40. {
  41. // These are reused by basic_outcome to save load on the compiler
  42. template <class value_type, class error_type> struct result_predicates
  43. {
  44. // Predicate for the implicit constructors to be available. Weakened to allow result<int, C enum>.
  45. static constexpr bool implicit_constructors_enabled = //
  46. !(trait::is_error_type<std::decay_t<value_type>>::value &&
  47. trait::is_error_type<std::decay_t<error_type>>::value) // both value and error types are not whitelisted error types
  48. && ((!detail::is_implicitly_constructible<value_type, error_type> &&
  49. !detail::is_implicitly_constructible<error_type, value_type>) // if value and error types cannot be constructed into one another
  50. || (trait::is_error_type<std::decay_t<error_type>>::value // if error type is a whitelisted error type
  51. && !detail::is_implicitly_constructible<error_type, value_type> // AND which cannot be constructed from the value type
  52. && std::is_integral<value_type>::value)); // AND the value type is some integral type
  53. // Predicate for the value converting constructor to be available. Weakened to allow result<int, C enum>.
  54. template <class T>
  55. static constexpr bool enable_value_converting_constructor = //
  56. implicit_constructors_enabled //
  57. && !is_in_place_type_t<std::decay_t<T>>::value // not in place construction
  58. && !trait::is_error_type_enum<error_type, std::decay_t<T>>::value // not an enum valid for my error type
  59. && ((detail::is_implicitly_constructible<value_type, T> && !detail::is_implicitly_constructible<error_type, T>) // is unambiguously for value type
  60. || (std::is_same<value_type, std::decay_t<T>>::value // OR is my value type exactly
  61. && detail::is_implicitly_constructible<value_type, T>) ); // and my value type is constructible from this ref form of T
  62. // Predicate for the error converting constructor to be available. Weakened to allow result<int, C enum>.
  63. template <class T>
  64. static constexpr bool enable_error_converting_constructor = //
  65. implicit_constructors_enabled //
  66. && !is_in_place_type_t<std::decay_t<T>>::value // not in place construction
  67. && !trait::is_error_type_enum<error_type, std::decay_t<T>>::value // not an enum valid for my error type
  68. && ((!detail::is_implicitly_constructible<value_type, T> && detail::is_implicitly_constructible<error_type, T>) // is unambiguously for error type
  69. || (std::is_same<error_type, std::decay_t<T>>::value // OR is my error type exactly
  70. && detail::is_implicitly_constructible<error_type, T>) ); // and my error type is constructible from this ref form of T
  71. // Predicate for the error condition converting constructor to be available.
  72. template <class ErrorCondEnum>
  73. static constexpr bool enable_error_condition_converting_constructor = //
  74. !is_in_place_type_t<std::decay_t<ErrorCondEnum>>::value // not in place construction
  75. && trait::is_error_type_enum<error_type, std::decay_t<ErrorCondEnum>>::value // is an error condition enum
  76. /*&& !detail::is_implicitly_constructible<value_type, ErrorCondEnum> && !detail::is_implicitly_constructible<error_type, ErrorCondEnum>*/; // not
  77. // constructible
  78. // via any other
  79. // means
  80. // Predicate for the converting constructor from a compatible input to be available.
  81. template <class T, class U, class V>
  82. static constexpr bool enable_compatible_conversion = //
  83. (std::is_void<T>::value ||
  84. detail::is_explicitly_constructible<value_type, typename basic_result<T, U, V>::value_type>) // if our value types are constructible
  85. &&(std::is_void<U>::value ||
  86. detail::is_explicitly_constructible<error_type, typename basic_result<T, U, V>::error_type>) // if our error types are constructible
  87. ;
  88. // Predicate for the converting constructor from a make_error_code() of the input to be available.
  89. template <class T, class U, class V>
  90. static constexpr bool enable_make_error_code_compatible_conversion = //
  91. trait::is_error_code_available<std::decay_t<error_type>>::value // if error type has an error code
  92. && !enable_compatible_conversion<T, U, V> // and the normal compatible conversion is not available
  93. && (std::is_void<T>::value ||
  94. detail::is_explicitly_constructible<value_type, typename basic_result<T, U, V>::value_type>) // and if our value types are constructible
  95. &&detail::is_explicitly_constructible<error_type,
  96. typename trait::is_error_code_available<U>::type>; // and our error type is constructible from a make_error_code()
  97. // Predicate for the converting constructor from a make_exception_ptr() of the input to be available.
  98. template <class T, class U, class V>
  99. static constexpr bool enable_make_exception_ptr_compatible_conversion = //
  100. trait::is_exception_ptr_available<std::decay_t<error_type>>::value // if error type has an exception ptr
  101. && !enable_compatible_conversion<T, U, V> // and the normal compatible conversion is not available
  102. && (std::is_void<T>::value ||
  103. detail::is_explicitly_constructible<value_type, typename basic_result<T, U, V>::value_type>) // and if our value types are constructible
  104. &&detail::is_explicitly_constructible<error_type, typename trait::is_exception_ptr_available<U>::type>; // and our error type is constructible from a
  105. // make_exception_ptr()
  106. // Predicate for the implicit converting inplace constructor from a compatible input to be available.
  107. struct disable_inplace_value_error_constructor;
  108. template <class... Args>
  109. using choose_inplace_value_error_constructor = std::conditional_t< //
  110. std::is_constructible<value_type, Args...>::value && std::is_constructible<error_type, Args...>::value, //
  111. disable_inplace_value_error_constructor, //
  112. std::conditional_t< //
  113. std::is_constructible<value_type, Args...>::value, //
  114. value_type, //
  115. std::conditional_t< //
  116. std::is_constructible<error_type, Args...>::value, //
  117. error_type, //
  118. disable_inplace_value_error_constructor>>>;
  119. template <class... Args>
  120. static constexpr bool enable_inplace_value_error_constructor =
  121. implicit_constructors_enabled //
  122. && !std::is_same<choose_inplace_value_error_constructor<Args...>, disable_inplace_value_error_constructor>::value;
  123. };
  124. template <class T, class U> constexpr inline const U &extract_value_from_success(const success_type<U> &v) { return v.value(); }
  125. template <class T, class U> constexpr inline U &&extract_value_from_success(success_type<U> &&v) { return static_cast<success_type<U> &&>(v).value(); }
  126. template <class T> constexpr inline T extract_value_from_success(const success_type<void> & /*unused*/) { return T{}; }
  127. template <class T, class U, class V> constexpr inline const U &extract_error_from_failure(const failure_type<U, V> &v) { return v.error(); }
  128. template <class T, class U, class V> constexpr inline U &&extract_error_from_failure(failure_type<U, V> &&v)
  129. {
  130. return static_cast<failure_type<U, V> &&>(v).error();
  131. }
  132. template <class T, class V> constexpr inline T extract_error_from_failure(const failure_type<void, V> & /*unused*/) { return T{}; }
  133. template <class T> struct is_basic_result
  134. {
  135. static constexpr bool value = false;
  136. };
  137. template <class R, class S, class T> struct is_basic_result<basic_result<R, S, T>>
  138. {
  139. static constexpr bool value = true;
  140. };
  141. } // namespace detail
  142. /*! AWAITING HUGO JSON CONVERSION TOOL
  143. type alias template <class T> is_basic_result. Potential doc page: `is_basic_result<T>`
  144. */
  145. template <class T> using is_basic_result = detail::is_basic_result<std::decay_t<T>>;
  146. /*! AWAITING HUGO JSON CONVERSION TOOL
  147. SIGNATURE NOT RECOGNISED
  148. */
  149. template <class T> static constexpr bool is_basic_result_v = detail::is_basic_result<std::decay_t<T>>::value;
  150. /*! AWAITING HUGO JSON CONVERSION TOOL
  151. SIGNATURE NOT RECOGNISED
  152. */
  153. namespace hooks
  154. {
  155. /*! AWAITING HUGO JSON CONVERSION TOOL
  156. SIGNATURE NOT RECOGNISED
  157. */
  158. template <class T, class U> constexpr inline void hook_result_construction(T * /*unused*/, U && /*unused*/) noexcept {}
  159. /*! AWAITING HUGO JSON CONVERSION TOOL
  160. SIGNATURE NOT RECOGNISED
  161. */
  162. template <class T, class U> constexpr inline void hook_result_copy_construction(T * /*unused*/, U && /*unused*/) noexcept {}
  163. /*! AWAITING HUGO JSON CONVERSION TOOL
  164. SIGNATURE NOT RECOGNISED
  165. */
  166. template <class T, class U> constexpr inline void hook_result_move_construction(T * /*unused*/, U && /*unused*/) noexcept {}
  167. /*! AWAITING HUGO JSON CONVERSION TOOL
  168. SIGNATURE NOT RECOGNISED
  169. */
  170. template <class T, class U, class... Args>
  171. constexpr inline void hook_result_in_place_construction(T * /*unused*/, in_place_type_t<U> /*unused*/, Args &&... /*unused*/) noexcept
  172. {
  173. }
  174. /*! AWAITING HUGO JSON CONVERSION TOOL
  175. SIGNATURE NOT RECOGNISED
  176. */
  177. template <class R, class S, class NoValuePolicy> constexpr inline uint16_t spare_storage(const detail::basic_result_final<R, S, NoValuePolicy> *r) noexcept
  178. {
  179. return r->_state._status.spare_storage_value;
  180. }
  181. /*! AWAITING HUGO JSON CONVERSION TOOL
  182. SIGNATURE NOT RECOGNISED
  183. */
  184. template <class R, class S, class NoValuePolicy>
  185. constexpr inline void set_spare_storage(detail::basic_result_final<R, S, NoValuePolicy> *r, uint16_t v) noexcept
  186. {
  187. r->_state._status.spare_storage_value = v;
  188. }
  189. } // namespace hooks
  190. /*! AWAITING HUGO JSON CONVERSION TOOL
  191. type definition template <class R, class S, class NoValuePolicy> basic_result. Potential doc page: `basic_result<T, E, NoValuePolicy>`
  192. */
  193. template <class R, class S, class NoValuePolicy> //
  194. class BOOST_OUTCOME_NODISCARD basic_result : public detail::basic_result_final<R, S, NoValuePolicy>
  195. {
  196. static_assert(trait::type_can_be_used_in_basic_result<R>, "The type R cannot be used in a basic_result");
  197. static_assert(trait::type_can_be_used_in_basic_result<S>, "The type S cannot be used in a basic_result");
  198. static_assert(std::is_void<S>::value || std::is_default_constructible<S>::value, "The type S must be void or default constructible");
  199. using base = detail::basic_result_final<R, S, NoValuePolicy>;
  200. struct implicit_constructors_disabled_tag
  201. {
  202. };
  203. struct value_converting_constructor_tag
  204. {
  205. };
  206. struct error_converting_constructor_tag
  207. {
  208. };
  209. struct error_condition_converting_constructor_tag
  210. {
  211. };
  212. struct explicit_valueornone_converting_constructor_tag
  213. {
  214. };
  215. struct explicit_valueorerror_converting_constructor_tag
  216. {
  217. };
  218. struct explicit_compatible_copy_conversion_tag
  219. {
  220. };
  221. struct explicit_compatible_move_conversion_tag
  222. {
  223. };
  224. struct explicit_make_error_code_compatible_copy_conversion_tag
  225. {
  226. };
  227. struct explicit_make_error_code_compatible_move_conversion_tag
  228. {
  229. };
  230. struct explicit_make_exception_ptr_compatible_copy_conversion_tag
  231. {
  232. };
  233. struct explicit_make_exception_ptr_compatible_move_conversion_tag
  234. {
  235. };
  236. public:
  237. using value_type = R;
  238. using error_type = S;
  239. using value_type_if_enabled = typename base::_value_type;
  240. using error_type_if_enabled = typename base::_error_type;
  241. template <class T, class U = S, class V = NoValuePolicy> using rebind = basic_result<T, U, V>;
  242. protected:
  243. // Requirement predicates for result.
  244. struct predicate
  245. {
  246. using base = detail::result_predicates<value_type, error_type>;
  247. // Predicate for any constructors to be available at all
  248. static constexpr bool constructors_enabled = !std::is_same<std::decay_t<value_type>, std::decay_t<error_type>>::value;
  249. // Predicate for implicit constructors to be available at all
  250. static constexpr bool implicit_constructors_enabled = constructors_enabled && base::implicit_constructors_enabled;
  251. // Predicate for the value converting constructor to be available.
  252. template <class T>
  253. static constexpr bool enable_value_converting_constructor = //
  254. constructors_enabled //
  255. && !std::is_same<std::decay_t<T>, basic_result>::value // not my type
  256. && base::template enable_value_converting_constructor<T>;
  257. // Predicate for the error converting constructor to be available.
  258. template <class T>
  259. static constexpr bool enable_error_converting_constructor = //
  260. constructors_enabled //
  261. && !std::is_same<std::decay_t<T>, basic_result>::value // not my type
  262. && base::template enable_error_converting_constructor<T>;
  263. // Predicate for the error condition converting constructor to be available.
  264. template <class ErrorCondEnum>
  265. static constexpr bool enable_error_condition_converting_constructor = //
  266. constructors_enabled //
  267. && !std::is_same<std::decay_t<ErrorCondEnum>, basic_result>::value // not my type
  268. && base::template enable_error_condition_converting_constructor<ErrorCondEnum>;
  269. // Predicate for the converting constructor from a compatible input to be available.
  270. template <class T, class U, class V>
  271. static constexpr bool enable_compatible_conversion = //
  272. constructors_enabled //
  273. && !std::is_same<basic_result<T, U, V>, basic_result>::value // not my type
  274. && base::template enable_compatible_conversion<T, U, V>;
  275. // Predicate for the converting constructor from a make_error_code() of the input to be available.
  276. template <class T, class U, class V>
  277. static constexpr bool enable_make_error_code_compatible_conversion = //
  278. constructors_enabled //
  279. && !std::is_same<basic_result<T, U, V>, basic_result>::value // not my type
  280. && base::template enable_make_error_code_compatible_conversion<T, U, V>;
  281. // Predicate for the converting constructor from a make_exception_ptr() of the input to be available.
  282. template <class T, class U, class V>
  283. static constexpr bool enable_make_exception_ptr_compatible_conversion = //
  284. constructors_enabled //
  285. && !std::is_same<basic_result<T, U, V>, basic_result>::value // not my type
  286. && base::template enable_make_exception_ptr_compatible_conversion<T, U, V>;
  287. // Predicate for the inplace construction of value to be available.
  288. template <class... Args>
  289. static constexpr bool enable_inplace_value_constructor = //
  290. constructors_enabled //
  291. && (std::is_void<value_type>::value //
  292. || std::is_constructible<value_type, Args...>::value);
  293. // Predicate for the inplace construction of error to be available.
  294. template <class... Args>
  295. static constexpr bool enable_inplace_error_constructor = //
  296. constructors_enabled //
  297. && (std::is_void<error_type>::value //
  298. || std::is_constructible<error_type, Args...>::value);
  299. // Predicate for the implicit converting inplace constructor to be available.
  300. template <class... Args>
  301. static constexpr bool enable_inplace_value_error_constructor = //
  302. constructors_enabled //
  303. &&base::template enable_inplace_value_error_constructor<Args...>;
  304. template <class... Args> using choose_inplace_value_error_constructor = typename base::template choose_inplace_value_error_constructor<Args...>;
  305. };
  306. public:
  307. /*! AWAITING HUGO JSON CONVERSION TOOL
  308. SIGNATURE NOT RECOGNISED
  309. */
  310. basic_result() = delete;
  311. /*! AWAITING HUGO JSON CONVERSION TOOL
  312. SIGNATURE NOT RECOGNISED
  313. */
  314. basic_result(basic_result && /*unused*/) = default; // NOLINT
  315. /*! AWAITING HUGO JSON CONVERSION TOOL
  316. SIGNATURE NOT RECOGNISED
  317. */
  318. basic_result(const basic_result & /*unused*/) = default;
  319. /*! AWAITING HUGO JSON CONVERSION TOOL
  320. SIGNATURE NOT RECOGNISED
  321. */
  322. basic_result &operator=(basic_result && /*unused*/) = default; // NOLINT
  323. /*! AWAITING HUGO JSON CONVERSION TOOL
  324. SIGNATURE NOT RECOGNISED
  325. */
  326. basic_result &operator=(const basic_result & /*unused*/) = default;
  327. ~basic_result() = default;
  328. /*! AWAITING HUGO JSON CONVERSION TOOL
  329. SIGNATURE NOT RECOGNISED
  330. */
  331. BOOST_OUTCOME_TEMPLATE(class Arg, class... Args)
  332. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!predicate::constructors_enabled && (sizeof...(Args) >= 0)))
  333. basic_result(Arg && /*unused*/, Args &&... /*unused*/) = delete; // NOLINT basic_result<T, T> is NOT SUPPORTED, see docs!
  334. /*! AWAITING HUGO JSON CONVERSION TOOL
  335. SIGNATURE NOT RECOGNISED
  336. */
  337. BOOST_OUTCOME_TEMPLATE(class T)
  338. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED((predicate::constructors_enabled && !predicate::implicit_constructors_enabled //
  339. && (detail::is_implicitly_constructible<value_type, T> || detail::is_implicitly_constructible<error_type, T>) )))
  340. basic_result(T && /*unused*/, implicit_constructors_disabled_tag /*unused*/ = implicit_constructors_disabled_tag()) =
  341. delete; // NOLINT Implicit constructors disabled, use explicit in_place_type<T>, success() or failure(). see docs!
  342. /*! AWAITING HUGO JSON CONVERSION TOOL
  343. SIGNATURE NOT RECOGNISED
  344. */
  345. BOOST_OUTCOME_TEMPLATE(class T)
  346. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_value_converting_constructor<T>))
  347. constexpr basic_result(T &&t, value_converting_constructor_tag /*unused*/ = value_converting_constructor_tag()) noexcept(
  348. std::is_nothrow_constructible<value_type, T>::value) // NOLINT
  349. : base{in_place_type<typename base::value_type>, static_cast<T &&>(t)}
  350. {
  351. using namespace hooks;
  352. hook_result_construction(this, static_cast<T &&>(t));
  353. }
  354. /*! AWAITING HUGO JSON CONVERSION TOOL
  355. SIGNATURE NOT RECOGNISED
  356. */
  357. BOOST_OUTCOME_TEMPLATE(class T)
  358. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_error_converting_constructor<T>))
  359. constexpr basic_result(T &&t, error_converting_constructor_tag /*unused*/ = error_converting_constructor_tag()) noexcept(
  360. std::is_nothrow_constructible<error_type, T>::value) // NOLINT
  361. : base{in_place_type<typename base::error_type>, static_cast<T &&>(t)}
  362. {
  363. using namespace hooks;
  364. hook_result_construction(this, static_cast<T &&>(t));
  365. }
  366. /*! AWAITING HUGO JSON CONVERSION TOOL
  367. SIGNATURE NOT RECOGNISED
  368. */
  369. BOOST_OUTCOME_TEMPLATE(class ErrorCondEnum)
  370. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(error_type(make_error_code(ErrorCondEnum()))), //
  371. BOOST_OUTCOME_TPRED(predicate::template enable_error_condition_converting_constructor<ErrorCondEnum>))
  372. constexpr basic_result(ErrorCondEnum &&t, error_condition_converting_constructor_tag /*unused*/ = error_condition_converting_constructor_tag()) noexcept(
  373. noexcept(error_type(make_error_code(static_cast<ErrorCondEnum &&>(t))))) // NOLINT
  374. : base{in_place_type<typename base::error_type>, make_error_code(t)}
  375. {
  376. using namespace hooks;
  377. hook_result_construction(this, static_cast<ErrorCondEnum &&>(t));
  378. }
  379. /*! AWAITING HUGO JSON CONVERSION TOOL
  380. SIGNATURE NOT RECOGNISED
  381. */
  382. BOOST_OUTCOME_TEMPLATE(class T)
  383. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(convert::value_or_error<basic_result, std::decay_t<T>>::enable_result_inputs || !is_basic_result_v<T>), //
  384. BOOST_OUTCOME_TEXPR(convert::value_or_error<basic_result, std::decay_t<T>>{}(std::declval<T>())))
  385. constexpr explicit basic_result(T &&o,
  386. explicit_valueorerror_converting_constructor_tag /*unused*/ = explicit_valueorerror_converting_constructor_tag()) // NOLINT
  387. : basic_result{convert::value_or_error<basic_result, std::decay_t<T>>{}(static_cast<T &&>(o))}
  388. {
  389. }
  390. /*! AWAITING HUGO JSON CONVERSION TOOL
  391. SIGNATURE NOT RECOGNISED
  392. */
  393. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  394. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_compatible_conversion<T, U, V>))
  395. constexpr explicit basic_result(
  396. const basic_result<T, U, V> &o,
  397. explicit_compatible_copy_conversion_tag /*unused*/ =
  398. explicit_compatible_copy_conversion_tag()) noexcept(std::is_nothrow_constructible<value_type, T>::value &&std::is_nothrow_constructible<error_type, U>::value)
  399. : base{typename base::compatible_conversion_tag(), o}
  400. {
  401. using namespace hooks;
  402. hook_result_copy_construction(this, o);
  403. }
  404. /*! AWAITING HUGO JSON CONVERSION TOOL
  405. SIGNATURE NOT RECOGNISED
  406. */
  407. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  408. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_compatible_conversion<T, U, V>))
  409. constexpr explicit basic_result(
  410. basic_result<T, U, V> &&o,
  411. explicit_compatible_move_conversion_tag /*unused*/ =
  412. explicit_compatible_move_conversion_tag()) noexcept(std::is_nothrow_constructible<value_type, T>::value &&std::is_nothrow_constructible<error_type, U>::value)
  413. : base{typename base::compatible_conversion_tag(), static_cast<basic_result<T, U, V> &&>(o)}
  414. {
  415. using namespace hooks;
  416. hook_result_move_construction(this, static_cast<basic_result<T, U, V> &&>(o));
  417. }
  418. /*! AWAITING HUGO JSON CONVERSION TOOL
  419. SIGNATURE NOT RECOGNISED
  420. */
  421. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  422. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_error_code_compatible_conversion<T, U, V>))
  423. constexpr explicit basic_result(const basic_result<T, U, V> &o,
  424. explicit_make_error_code_compatible_copy_conversion_tag /*unused*/ =
  425. explicit_make_error_code_compatible_copy_conversion_tag()) noexcept(std::is_nothrow_constructible<value_type, T>::value
  426. &&noexcept(make_error_code(std::declval<U>())))
  427. : base{typename base::make_error_code_compatible_conversion_tag(), o}
  428. {
  429. using namespace hooks;
  430. hook_result_copy_construction(this, o);
  431. }
  432. /*! AWAITING HUGO JSON CONVERSION TOOL
  433. SIGNATURE NOT RECOGNISED
  434. */
  435. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  436. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_error_code_compatible_conversion<T, U, V>))
  437. constexpr explicit basic_result(basic_result<T, U, V> &&o,
  438. explicit_make_error_code_compatible_move_conversion_tag /*unused*/ =
  439. explicit_make_error_code_compatible_move_conversion_tag()) noexcept(std::is_nothrow_constructible<value_type, T>::value
  440. &&noexcept(make_error_code(std::declval<U>())))
  441. : base{typename base::make_error_code_compatible_conversion_tag(), static_cast<basic_result<T, U, V> &&>(o)}
  442. {
  443. using namespace hooks;
  444. hook_result_move_construction(this, static_cast<basic_result<T, U, V> &&>(o));
  445. }
  446. /*! AWAITING HUGO JSON CONVERSION TOOL
  447. SIGNATURE NOT RECOGNISED
  448. */
  449. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  450. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_exception_ptr_compatible_conversion<T, U, V>))
  451. constexpr explicit basic_result(const basic_result<T, U, V> &o,
  452. explicit_make_exception_ptr_compatible_copy_conversion_tag /*unused*/ =
  453. explicit_make_exception_ptr_compatible_copy_conversion_tag()) noexcept(std::is_nothrow_constructible<value_type, T>::value
  454. &&noexcept(make_exception_ptr(std::declval<U>())))
  455. : base{typename base::make_exception_ptr_compatible_conversion_tag(), o}
  456. {
  457. using namespace hooks;
  458. hook_result_copy_construction(this, o);
  459. }
  460. /*! AWAITING HUGO JSON CONVERSION TOOL
  461. SIGNATURE NOT RECOGNISED
  462. */
  463. BOOST_OUTCOME_TEMPLATE(class T, class U, class V)
  464. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_exception_ptr_compatible_conversion<T, U, V>))
  465. constexpr explicit basic_result(basic_result<T, U, V> &&o,
  466. explicit_make_exception_ptr_compatible_move_conversion_tag /*unused*/ =
  467. explicit_make_exception_ptr_compatible_move_conversion_tag()) noexcept(std::is_nothrow_constructible<value_type, T>::value
  468. &&noexcept(make_exception_ptr(std::declval<U>())))
  469. : base{typename base::make_exception_ptr_compatible_conversion_tag(), static_cast<basic_result<T, U, V> &&>(o)}
  470. {
  471. using namespace hooks;
  472. hook_result_move_construction(this, static_cast<basic_result<T, U, V> &&>(o));
  473. }
  474. /*! AWAITING HUGO JSON CONVERSION TOOL
  475. SIGNATURE NOT RECOGNISED
  476. */
  477. BOOST_OUTCOME_TEMPLATE(class... Args)
  478. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_inplace_value_constructor<Args...>))
  479. constexpr explicit basic_result(in_place_type_t<value_type_if_enabled> _, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args...>::value)
  480. : base{_, static_cast<Args &&>(args)...}
  481. {
  482. using namespace hooks;
  483. hook_result_in_place_construction(this, in_place_type<value_type>, static_cast<Args &&>(args)...);
  484. }
  485. /*! AWAITING HUGO JSON CONVERSION TOOL
  486. SIGNATURE NOT RECOGNISED
  487. */
  488. BOOST_OUTCOME_TEMPLATE(class U, class... Args)
  489. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_inplace_value_constructor<std::initializer_list<U>, Args...>))
  490. constexpr explicit basic_result(in_place_type_t<value_type_if_enabled> _, std::initializer_list<U> il,
  491. Args &&... args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<U>, Args...>::value)
  492. : base{_, il, static_cast<Args &&>(args)...}
  493. {
  494. using namespace hooks;
  495. hook_result_in_place_construction(this, in_place_type<value_type>, il, static_cast<Args &&>(args)...);
  496. }
  497. /*! AWAITING HUGO JSON CONVERSION TOOL
  498. SIGNATURE NOT RECOGNISED
  499. */
  500. BOOST_OUTCOME_TEMPLATE(class... Args)
  501. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_inplace_error_constructor<Args...>))
  502. constexpr explicit basic_result(in_place_type_t<error_type_if_enabled> _, Args &&... args) noexcept(std::is_nothrow_constructible<error_type, Args...>::value)
  503. : base{_, static_cast<Args &&>(args)...}
  504. {
  505. using namespace hooks;
  506. hook_result_in_place_construction(this, in_place_type<error_type>, static_cast<Args &&>(args)...);
  507. }
  508. /*! AWAITING HUGO JSON CONVERSION TOOL
  509. SIGNATURE NOT RECOGNISED
  510. */
  511. BOOST_OUTCOME_TEMPLATE(class U, class... Args)
  512. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_inplace_error_constructor<std::initializer_list<U>, Args...>))
  513. constexpr explicit basic_result(in_place_type_t<error_type_if_enabled> _, std::initializer_list<U> il,
  514. Args &&... args) noexcept(std::is_nothrow_constructible<error_type, std::initializer_list<U>, Args...>::value)
  515. : base{_, il, static_cast<Args &&>(args)...}
  516. {
  517. using namespace hooks;
  518. hook_result_in_place_construction(this, in_place_type<error_type>, il, static_cast<Args &&>(args)...);
  519. }
  520. /*! AWAITING HUGO JSON CONVERSION TOOL
  521. SIGNATURE NOT RECOGNISED
  522. */
  523. BOOST_OUTCOME_TEMPLATE(class A1, class A2, class... Args)
  524. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_inplace_value_error_constructor<A1, A2, Args...>))
  525. constexpr basic_result(A1 &&a1, A2 &&a2, Args &&... args) noexcept(noexcept(
  526. typename predicate::template choose_inplace_value_error_constructor<A1, A2, Args...>(std::declval<A1>(), std::declval<A2>(), std::declval<Args>()...)))
  527. : basic_result(in_place_type<typename predicate::template choose_inplace_value_error_constructor<A1, A2, Args...>>, static_cast<A1 &&>(a1),
  528. static_cast<A2 &&>(a2), static_cast<Args &&>(args)...)
  529. {
  530. /* I was a little surprised that the below is needed given that we forward to another constructor.
  531. But it turns out that ADL only fires on the first constructor for some reason.
  532. */
  533. using namespace hooks;
  534. // hook_result_in_place_construction(in_place_type<typename predicate::template choose_inplace_value_error_constructor<A1, A2, Args...>>, this);
  535. }
  536. /*! AWAITING HUGO JSON CONVERSION TOOL
  537. SIGNATURE NOT RECOGNISED
  538. */
  539. constexpr basic_result(const success_type<void> &o) noexcept(std::is_nothrow_default_constructible<value_type>::value) // NOLINT
  540. : base{in_place_type<value_type_if_enabled>}
  541. {
  542. using namespace hooks;
  543. hook_result_copy_construction(this, o);
  544. }
  545. /*! AWAITING HUGO JSON CONVERSION TOOL
  546. SIGNATURE NOT RECOGNISED
  547. */
  548. BOOST_OUTCOME_TEMPLATE(class T)
  549. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_compatible_conversion<T, void, void>))
  550. constexpr basic_result(const success_type<T> &o) noexcept(std::is_nothrow_constructible<value_type, T>::value) // NOLINT
  551. : base{in_place_type<value_type_if_enabled>, detail::extract_value_from_success<value_type>(o)}
  552. {
  553. using namespace hooks;
  554. hook_result_copy_construction(this, o);
  555. }
  556. /*! AWAITING HUGO JSON CONVERSION TOOL
  557. SIGNATURE NOT RECOGNISED
  558. */
  559. BOOST_OUTCOME_TEMPLATE(class T)
  560. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_void<T>::value && predicate::template enable_compatible_conversion<T, void, void>))
  561. constexpr basic_result(success_type<T> &&o) noexcept(std::is_nothrow_constructible<value_type, T>::value) // NOLINT
  562. : base{in_place_type<value_type_if_enabled>, detail::extract_value_from_success<value_type>(static_cast<success_type<T> &&>(o))}
  563. {
  564. using namespace hooks;
  565. hook_result_move_construction(this, static_cast<success_type<T> &&>(o));
  566. }
  567. /*! AWAITING HUGO JSON CONVERSION TOOL
  568. SIGNATURE NOT RECOGNISED
  569. */
  570. BOOST_OUTCOME_TEMPLATE(class T)
  571. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_compatible_conversion<void, T, void>))
  572. constexpr basic_result(const failure_type<T> &o, explicit_compatible_copy_conversion_tag /*unused*/ = explicit_compatible_copy_conversion_tag()) noexcept(
  573. std::is_nothrow_constructible<error_type, T>::value) // NOLINT
  574. : base{in_place_type<error_type_if_enabled>, detail::extract_error_from_failure<error_type>(o)}
  575. {
  576. using namespace hooks;
  577. hook_result_copy_construction(this, o);
  578. }
  579. /*! AWAITING HUGO JSON CONVERSION TOOL
  580. SIGNATURE NOT RECOGNISED
  581. */
  582. BOOST_OUTCOME_TEMPLATE(class T)
  583. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_compatible_conversion<void, T, void>))
  584. constexpr basic_result(failure_type<T> &&o, explicit_compatible_move_conversion_tag /*unused*/ = explicit_compatible_move_conversion_tag()) noexcept(
  585. std::is_nothrow_constructible<error_type, T>::value) // NOLINT
  586. : base{in_place_type<error_type_if_enabled>, detail::extract_error_from_failure<error_type>(static_cast<failure_type<T> &&>(o))}
  587. {
  588. using namespace hooks;
  589. hook_result_move_construction(this, static_cast<failure_type<T> &&>(o));
  590. }
  591. /*! AWAITING HUGO JSON CONVERSION TOOL
  592. SIGNATURE NOT RECOGNISED
  593. */
  594. BOOST_OUTCOME_TEMPLATE(class T)
  595. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_error_code_compatible_conversion<void, T, void>))
  596. constexpr basic_result(const failure_type<T> &o,
  597. explicit_make_error_code_compatible_copy_conversion_tag /*unused*/ =
  598. explicit_make_error_code_compatible_copy_conversion_tag()) noexcept(noexcept(make_error_code(std::declval<T>()))) // NOLINT
  599. : base{in_place_type<error_type_if_enabled>, make_error_code(detail::extract_error_from_failure<error_type>(o))}
  600. {
  601. using namespace hooks;
  602. hook_result_copy_construction(this, o);
  603. }
  604. /*! AWAITING HUGO JSON CONVERSION TOOL
  605. SIGNATURE NOT RECOGNISED
  606. */
  607. BOOST_OUTCOME_TEMPLATE(class T)
  608. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_error_code_compatible_conversion<void, T, void>))
  609. constexpr basic_result(failure_type<T> &&o,
  610. explicit_make_error_code_compatible_move_conversion_tag /*unused*/ =
  611. explicit_make_error_code_compatible_move_conversion_tag()) noexcept(noexcept(make_error_code(std::declval<T>()))) // NOLINT
  612. : base{in_place_type<error_type_if_enabled>, make_error_code(detail::extract_error_from_failure<error_type>(static_cast<failure_type<T> &&>(o)))}
  613. {
  614. using namespace hooks;
  615. hook_result_move_construction(this, static_cast<failure_type<T> &&>(o));
  616. }
  617. /*! AWAITING HUGO JSON CONVERSION TOOL
  618. SIGNATURE NOT RECOGNISED
  619. */
  620. BOOST_OUTCOME_TEMPLATE(class T)
  621. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_exception_ptr_compatible_conversion<void, T, void>))
  622. constexpr basic_result(const failure_type<T> &o,
  623. explicit_make_exception_ptr_compatible_copy_conversion_tag /*unused*/ =
  624. explicit_make_exception_ptr_compatible_copy_conversion_tag()) noexcept(noexcept(make_exception_ptr(std::declval<T>()))) // NOLINT
  625. : base{in_place_type<error_type_if_enabled>, make_exception_ptr(detail::extract_error_from_failure<error_type>(o))}
  626. {
  627. using namespace hooks;
  628. hook_result_copy_construction(this, o);
  629. }
  630. /*! AWAITING HUGO JSON CONVERSION TOOL
  631. SIGNATURE NOT RECOGNISED
  632. */
  633. BOOST_OUTCOME_TEMPLATE(class T)
  634. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(predicate::template enable_make_exception_ptr_compatible_conversion<void, T, void>))
  635. constexpr basic_result(failure_type<T> &&o,
  636. explicit_make_exception_ptr_compatible_move_conversion_tag /*unused*/ =
  637. explicit_make_exception_ptr_compatible_move_conversion_tag()) noexcept(noexcept(make_exception_ptr(std::declval<T>()))) // NOLINT
  638. : base{in_place_type<error_type_if_enabled>, make_exception_ptr(detail::extract_error_from_failure<error_type>(static_cast<failure_type<T> &&>(o)))}
  639. {
  640. using namespace hooks;
  641. hook_result_move_construction(this, static_cast<failure_type<T> &&>(o));
  642. }
  643. /*! AWAITING HUGO JSON CONVERSION TOOL
  644. SIGNATURE NOT RECOGNISED
  645. */
  646. constexpr void swap(basic_result &o) noexcept((std::is_void<value_type>::value || detail::is_nothrow_swappable<value_type>::value) //
  647. && (std::is_void<error_type>::value || detail::is_nothrow_swappable<error_type>::value))
  648. {
  649. constexpr bool value_throws = !std::is_void<value_type>::value && !detail::is_nothrow_swappable<value_type>::value;
  650. constexpr bool error_throws = !std::is_void<error_type>::value && !detail::is_nothrow_swappable<error_type>::value;
  651. detail::basic_result_storage_swap<value_throws, error_throws>(*this, o);
  652. }
  653. /*! AWAITING HUGO JSON CONVERSION TOOL
  654. SIGNATURE NOT RECOGNISED
  655. */
  656. auto as_failure() const & { return failure(this->assume_error()); }
  657. /*! AWAITING HUGO JSON CONVERSION TOOL
  658. SIGNATURE NOT RECOGNISED
  659. */
  660. auto as_failure() && { return failure(static_cast<basic_result &&>(*this).assume_error()); }
  661. };
  662. /*! AWAITING HUGO JSON CONVERSION TOOL
  663. SIGNATURE NOT RECOGNISED
  664. */
  665. template <class R, class S, class P> inline void swap(basic_result<R, S, P> &a, basic_result<R, S, P> &b) noexcept(noexcept(a.swap(b)))
  666. {
  667. a.swap(b);
  668. }
  669. #if !defined(NDEBUG)
  670. // Check is trivial in all ways except default constructibility
  671. // static_assert(std::is_trivial<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivial!");
  672. // static_assert(std::is_trivially_default_constructible<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially default
  673. // constructible!");
  674. static_assert(std::is_trivially_copyable<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially copyable!");
  675. static_assert(std::is_trivially_assignable<basic_result<int, long, policy::all_narrow>, basic_result<int, long, policy::all_narrow>>::value,
  676. "result<int> is not trivially assignable!");
  677. static_assert(std::is_trivially_destructible<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially destructible!");
  678. static_assert(std::is_trivially_copy_constructible<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially copy constructible!");
  679. static_assert(std::is_trivially_move_constructible<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially move constructible!");
  680. static_assert(std::is_trivially_copy_assignable<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially copy assignable!");
  681. static_assert(std::is_trivially_move_assignable<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not trivially move assignable!");
  682. // Also check is standard layout
  683. static_assert(std::is_standard_layout<basic_result<int, long, policy::all_narrow>>::value, "result<int> is not a standard layout type!");
  684. #endif
  685. BOOST_OUTCOME_V2_NAMESPACE_END
  686. #ifdef __clang__
  687. #pragma clang diagnostic pop
  688. #endif
  689. #endif