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 // [email protected] 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/lightweight_test.hpp" 20 #include "boost/none.hpp" 21 22 class NonConstructible 23 { 24 private: 25 NonConstructible(); 26 NonConstructible(NonConstructible const&); 27 #if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES) 28 NonConstructible(NonConstructible &&); 29 #endif 30 }; 31 test_non_constructible()32void test_non_constructible() 33 { 34 boost::optional<NonConstructible> o; 35 BOOST_TEST(!o); 36 BOOST_TEST(o == boost::none); 37 BOOST_TEST_THROWS(o.value(), boost::bad_optional_access); 38 } 39 40 class Guard 41 { 42 public: Guard(int)43 explicit Guard(int) {} 44 private: 45 Guard(); 46 Guard(Guard const&); 47 #if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES) 48 Guard(Guard &&); 49 #endif 50 }; 51 test_guard()52void test_guard() 53 { 54 boost::optional<Guard> o; 55 o.emplace(1); 56 BOOST_TEST(o); 57 BOOST_TEST(o != boost::none); 58 } 59 test_non_assignable()60void test_non_assignable() 61 { 62 boost::optional<const std::string> o; 63 o.emplace("cat"); 64 BOOST_TEST(o); 65 BOOST_TEST(o != boost::none); 66 BOOST_TEST_EQ(*o, std::string("cat")); 67 } 68 main()69int main() 70 { 71 test_non_constructible(); 72 test_guard(); 73 test_non_assignable(); 74 75 return boost::report_errors(); 76 }