noexcept-propagation.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Unit testing for outcomes
  2. (C) 2013-2019 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
  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/test/unit_test.hpp>
  26. #include <boost/test/unit_test_monitor.hpp>
  27. #ifdef _MSC_VER
  28. #pragma warning(disable : 4127) // conditional expression is constant
  29. #endif
  30. #ifndef BOOST_NO_EXCEPTIONS
  31. // std nothrow traits seem to return random values if exceptions are disabled on MSVC
  32. BOOST_OUTCOME_AUTO_TEST_CASE(works_outcome_noexcept, "Tests that the outcome correctly inherits noexcept from its type R")
  33. {
  34. using namespace BOOST_OUTCOME_V2_NAMESPACE;
  35. {
  36. using type = outcome<int>;
  37. BOOST_CHECK(std::is_nothrow_copy_constructible<type>::value);
  38. BOOST_CHECK(std::is_nothrow_move_constructible<type>::value);
  39. BOOST_CHECK(std::is_nothrow_copy_assignable<type>::value);
  40. BOOST_CHECK(std::is_nothrow_move_assignable<type>::value);
  41. BOOST_CHECK(std::is_nothrow_destructible<type>::value);
  42. }
  43. {
  44. using type = outcome<std::string>;
  45. BOOST_CHECK(std::is_nothrow_copy_constructible<type>::value == std::is_nothrow_copy_constructible<std::string>::value);
  46. BOOST_CHECK(std::is_nothrow_move_constructible<type>::value == std::is_nothrow_move_constructible<std::string>::value);
  47. BOOST_CHECK(std::is_nothrow_copy_assignable<type>::value == std::is_nothrow_copy_assignable<std::string>::value);
  48. BOOST_CHECK(std::is_nothrow_move_assignable<type>::value == std::is_nothrow_move_assignable<std::string>::value);
  49. BOOST_CHECK(std::is_nothrow_destructible<type>::value == std::is_nothrow_destructible<std::string>::value);
  50. }
  51. {
  52. struct Except
  53. {
  54. int n;
  55. Except() = delete;
  56. Except(const Except & /*unused*/) noexcept(false)
  57. : n(0)
  58. {
  59. }
  60. Except(Except && /*unused*/) noexcept(false)
  61. : n(0)
  62. {
  63. }
  64. Except &operator=(const Except & /*unused*/) noexcept(false) { return *this; }
  65. Except &operator=(Except && /*unused*/) noexcept(false) { return *this; }
  66. ~Except() noexcept(false) { n = 0; }
  67. };
  68. using type = outcome<Except>;
  69. BOOST_CHECK(!std::is_nothrow_copy_constructible<type>::value);
  70. BOOST_CHECK(!std::is_nothrow_move_constructible<type>::value);
  71. BOOST_CHECK(!std::is_nothrow_copy_assignable<type>::value);
  72. BOOST_CHECK(!std::is_nothrow_move_assignable<type>::value);
  73. BOOST_CHECK(!std::is_nothrow_destructible<type>::value);
  74. }
  75. }
  76. #endif