optional_test_ref_move.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2014 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: akrzemi1@gmail.com
  10. #include "boost/optional/optional.hpp"
  11. #ifdef __BORLANDC__
  12. #pragma hdrstop
  13. #endif
  14. #include <string>
  15. #include "boost/core/addressof.hpp"
  16. #include "boost/core/lightweight_test.hpp"
  17. using boost::optional;
  18. std::string global("text");
  19. optional<std::string&> make_optional_string_ref()
  20. {
  21. return optional<std::string&>(global);
  22. }
  23. std::string& return_global()
  24. {
  25. return global;
  26. }
  27. int main()
  28. {
  29. optional<std::string&> opt;
  30. opt = make_optional_string_ref();
  31. BOOST_TEST(bool(opt));
  32. BOOST_TEST(*opt == global);
  33. BOOST_TEST(boost::addressof(*opt) == boost::addressof(global));
  34. {
  35. std::string& str = *make_optional_string_ref();
  36. BOOST_TEST(str == global);
  37. BOOST_TEST(boost::addressof(str) == boost::addressof(global));
  38. }
  39. {
  40. std::string& str = make_optional_string_ref().value();
  41. BOOST_TEST(str == global);
  42. BOOST_TEST(boost::addressof(str) == boost::addressof(global));
  43. }
  44. {
  45. std::string& str = make_optional_string_ref().value_or(global);
  46. BOOST_TEST(str == global);
  47. BOOST_TEST(boost::addressof(str) == boost::addressof(global));
  48. }
  49. {
  50. std::string& str = make_optional_string_ref().value_or_eval(&return_global);
  51. BOOST_TEST(str == global);
  52. BOOST_TEST(boost::addressof(str) == boost::addressof(global));
  53. }
  54. return boost::report_errors();
  55. }