issue0210.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (1 commit)
  3. Boost Software License - Version 1.0 - August 17th, 2003
  4. Permission is hereby granted, free of charge, to any person or organization
  5. obtaining a copy of the software and accompanying documentation covered by
  6. this license (the "Software") to use, reproduce, display, distribute,
  7. execute, and transmit the Software, and to prepare derivative works of the
  8. Software, and to permit third-parties to whom the Software is furnished to
  9. do so, all subject to the following:
  10. The copyright notices in the Software and this entire statement, including
  11. the above license grant, this restriction and the following disclaimer,
  12. must be included in all copies of the Software, in whole or in part, and
  13. all derivative works of the Software, unless such copies or derivative
  14. works are solely in the form of machine-executable object code generated by
  15. a source language processor.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  19. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  20. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. DEALINGS IN THE SOFTWARE.
  23. */
  24. #include <boost/outcome/outcome.hpp>
  25. #include <boost/outcome/try.hpp>
  26. #include <boost/test/unit_test.hpp>
  27. #include <boost/test/unit_test_monitor.hpp>
  28. namespace issues210
  29. {
  30. namespace outcome = BOOST_OUTCOME_V2_NAMESPACE;
  31. struct error
  32. {
  33. boost::system::error_code code;
  34. };
  35. // say that my custom error code type is error code compatible
  36. inline boost::system::error_code make_error_code(error error) noexcept { return error.code; }
  37. template <typename T> using custom_result = outcome::basic_result<T, error, outcome::policy::default_policy<T, error, void>>;
  38. // source of custom result type with error code compatible error type
  39. inline custom_result<int> funcA(int x) { return x; }
  40. // Is the custom result type explicitly constructible to an ordinary result type?
  41. inline outcome::result<int> funcB(int x) { return outcome::result<int>(funcA(x)); }
  42. // Does the custom result type TRY-convert to an ordinary result type?
  43. inline outcome::result<int> func1(int x)
  44. {
  45. BOOST_OUTCOME_TRY(y, funcA(x));
  46. return funcB(y);
  47. }
  48. // Is the custom result type explicitly constructible to an ordinary outcome type?
  49. inline outcome::outcome<int> funcC(int x) { return outcome::outcome<int>(funcA(x)); }
  50. // Does the custom result type TRY-convert to an ordinary outcome type?
  51. inline outcome::outcome<int> func2(int x)
  52. {
  53. BOOST_OUTCOME_TRY(y, funcA(x));
  54. return funcC(y);
  55. }
  56. } // namespace issues210
  57. BOOST_OUTCOME_AUTO_TEST_CASE(issues_0210_test, "result<int, E> with error code compatible custom E does not TRY into a result<int, boost::system::error_code> function")
  58. {
  59. BOOST_CHECK(issues210::func1(5).value() == 5);
  60. BOOST_CHECK(issues210::func2(5).value() == 5);
  61. }