optional_test_minimum_requirements.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright (C) 2014-2015 Andrzej Krzemienski.
  2. //
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/lib/optional for documentation.
  8. //
  9. // You are welcome to contact the author at:
  10. // akrzemi1@gmail.com
  11. #include "boost/optional/optional.hpp"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "boost/core/lightweight_test.hpp"
  16. #include "boost/none.hpp"
  17. class NonConstructible
  18. {
  19. private:
  20. NonConstructible();
  21. NonConstructible(NonConstructible const&);
  22. #if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
  23. NonConstructible(NonConstructible &&);
  24. #endif
  25. };
  26. void test_non_constructible()
  27. {
  28. boost::optional<NonConstructible> o;
  29. BOOST_TEST(!o);
  30. BOOST_TEST(o == boost::none);
  31. BOOST_TEST_THROWS(o.value(), boost::bad_optional_access);
  32. }
  33. class Guard
  34. {
  35. public:
  36. explicit Guard(int) {}
  37. private:
  38. Guard();
  39. Guard(Guard const&);
  40. #if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
  41. Guard(Guard &&);
  42. #endif
  43. };
  44. void test_guard()
  45. {
  46. boost::optional<Guard> o;
  47. o.emplace(1);
  48. BOOST_TEST(o);
  49. BOOST_TEST(o != boost::none);
  50. }
  51. void test_non_assignable()
  52. {
  53. boost::optional<const std::string> o;
  54. o.emplace("cat");
  55. BOOST_TEST(o);
  56. BOOST_TEST(o != boost::none);
  57. BOOST_TEST_EQ(*o, std::string("cat"));
  58. }
  59. int main()
  60. {
  61. test_non_constructible();
  62. test_guard();
  63. test_non_assignable();
  64. return boost::report_errors();
  65. }