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: [email protected] 10 11 12 #include "boost/optional/optional.hpp" 13 14 #ifdef BOOST_BORLANDC 15 #pragma hdrstop 16 #endif 17 18 #include <string> 19 #include "boost/core/addressof.hpp" 20 #include "boost/core/lightweight_test.hpp" 21 22 using boost::optional; 23 24 std::string global("text"); 25 make_optional_string_ref()26optional<std::string&> make_optional_string_ref() 27 { 28 return optional<std::string&>(global); 29 } 30 return_global()31std::string& return_global() 32 { 33 return global; 34 } 35 main()36int main() 37 { 38 optional<std::string&> opt; 39 opt = make_optional_string_ref(); 40 BOOST_TEST(bool(opt)); 41 BOOST_TEST(*opt == global); 42 BOOST_TEST(boost::addressof(*opt) == boost::addressof(global)); 43 44 { 45 std::string& str = *make_optional_string_ref(); 46 BOOST_TEST(str == global); 47 BOOST_TEST(boost::addressof(str) == boost::addressof(global)); 48 } 49 50 { 51 std::string& str = make_optional_string_ref().value(); 52 BOOST_TEST(str == global); 53 BOOST_TEST(boost::addressof(str) == boost::addressof(global)); 54 } 55 56 { 57 std::string& str = make_optional_string_ref().value_or(global); 58 BOOST_TEST(str == global); 59 BOOST_TEST(boost::addressof(str) == boost::addressof(global)); 60 } 61 62 { 63 std::string& str = make_optional_string_ref().value_or_eval(&return_global); 64 BOOST_TEST(str == global); 65 BOOST_TEST(boost::addressof(str) == boost::addressof(global)); 66 } 67 68 return boost::report_errors(); 69 } 70