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