optional_test_convert_from_T.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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:
  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. //#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
  18. using boost::optional;
  19. using boost::none;
  20. template <typename U>
  21. struct superconv
  22. {
  23. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  24. template <typename T>
  25. superconv(T&&) { BOOST_STATIC_ASSERT(sizeof(T) == 0); }
  26. #else
  27. template <typename T>
  28. superconv(const T&) { BOOST_STATIC_ASSERT(sizeof(T) == 0); }
  29. template <typename T>
  30. superconv( T&) { BOOST_STATIC_ASSERT(sizeof(T) == 0); }
  31. #endif
  32. superconv() {}
  33. };
  34. void test_optional_of_superconverting_T() // compile-time test
  35. {
  36. #ifndef BOOST_OPTIONAL_DETAIL_NO_IS_CONSTRUCTIBLE_TRAIT
  37. superconv<optional<int> > s;
  38. superconv<optional<int> > & rs = s;
  39. optional<superconv<optional<int> > > os = rs;
  40. #endif
  41. }
  42. void test_optional_optional_T()
  43. {
  44. optional<int> oi1 (1), oiN;
  45. optional< optional<int> > ooi1 (oi1), ooiN(oiN);
  46. BOOST_TEST(ooi1);
  47. BOOST_TEST(*ooi1);
  48. BOOST_TEST_EQ(**ooi1, 1);
  49. BOOST_TEST(ooiN);
  50. BOOST_TEST(!*ooiN);
  51. }
  52. int main()
  53. {
  54. test_optional_optional_T();
  55. test_optional_of_superconverting_T();
  56. return boost::report_errors();
  57. }