optional_test_tie.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
  2. // Copyright (C) 2015 Andrzej Krzemienski.
  3. //
  4. // Use, modification, and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/lib/optional for documentation.
  9. //
  10. // You are welcome to contact the author at:
  11. // fernando_cacciola@hotmail.com
  12. #include "boost/optional/optional.hpp"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #include "boost/core/lightweight_test.hpp"
  17. #include "boost/none.hpp"
  18. #include "boost/tuple/tuple.hpp"
  19. struct counting_oracle
  20. {
  21. int val;
  22. counting_oracle() : val() { ++default_ctor_count; }
  23. counting_oracle(int v) : val(v) { ++val_ctor_count; }
  24. counting_oracle(const counting_oracle& rhs) : val(rhs.val) { ++copy_ctor_count; }
  25. counting_oracle& operator=(const counting_oracle& rhs) { val = rhs.val; ++copy_assign_count; return *this; }
  26. ~counting_oracle() { ++dtor_count; }
  27. static int dtor_count;
  28. static int default_ctor_count;
  29. static int val_ctor_count;
  30. static int copy_ctor_count;
  31. static int copy_assign_count;
  32. static int equals_count;
  33. friend bool operator==(const counting_oracle& lhs, const counting_oracle& rhs) { ++equals_count; return lhs.val == rhs.val; }
  34. static void clear_count()
  35. {
  36. dtor_count = default_ctor_count = val_ctor_count = copy_ctor_count = copy_assign_count = equals_count = 0;
  37. }
  38. };
  39. int counting_oracle::dtor_count = 0;
  40. int counting_oracle::default_ctor_count = 0;
  41. int counting_oracle::val_ctor_count = 0;
  42. int counting_oracle::copy_ctor_count = 0;
  43. int counting_oracle::copy_assign_count = 0;
  44. int counting_oracle::equals_count = 0;
  45. // Test boost::tie() interoperability.
  46. int main()
  47. {
  48. const std::pair<counting_oracle, counting_oracle> pair(1, 2);
  49. counting_oracle::clear_count();
  50. boost::optional<counting_oracle> o1, o2;
  51. boost::tie(o1, o2) = pair;
  52. BOOST_TEST(o1);
  53. BOOST_TEST(o2);
  54. BOOST_TEST(*o1 == counting_oracle(1));
  55. BOOST_TEST(*o2 == counting_oracle(2));
  56. BOOST_TEST_EQ(2, counting_oracle::copy_ctor_count);
  57. BOOST_TEST_EQ(0, counting_oracle::copy_assign_count);
  58. BOOST_TEST_EQ(0, counting_oracle::default_ctor_count);
  59. return boost::report_errors();
  60. }