cnstr.memberwise.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright Louis Dionne 2013-2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  4. #include <boost/hana/assert.hpp>
  5. #include <boost/hana/first.hpp>
  6. #include <boost/hana/pair.hpp>
  7. #include <boost/hana/second.hpp>
  8. namespace hana = boost::hana;
  9. struct MoveOnly {
  10. int data_;
  11. MoveOnly(MoveOnly const&) = delete;
  12. MoveOnly& operator=(MoveOnly const&) = delete;
  13. MoveOnly(int data) : data_(data) { }
  14. MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; }
  15. MoveOnly& operator=(MoveOnly&& x)
  16. { data_ = x.data_; x.data_ = 0; return *this; }
  17. bool operator==(const MoveOnly& x) const { return data_ == x.data_; }
  18. };
  19. class FromInt {
  20. int data_;
  21. public:
  22. constexpr FromInt(int data) : data_(data) { }
  23. constexpr bool operator==(const FromInt& x) const { return data_ == x.data_; }
  24. };
  25. int main() {
  26. //////////////////////////////////////////////////////////////////////////
  27. // Check for the pair(T&&, U&&) constructor
  28. //////////////////////////////////////////////////////////////////////////
  29. {
  30. hana::pair<MoveOnly, short*> p(MoveOnly{3}, nullptr);
  31. BOOST_HANA_RUNTIME_CHECK(hana::first(p) == MoveOnly{3});
  32. BOOST_HANA_RUNTIME_CHECK(hana::second(p) == nullptr);
  33. }
  34. //////////////////////////////////////////////////////////////////////////
  35. // Check for the pair(First const&, Second const&) constructor
  36. //////////////////////////////////////////////////////////////////////////
  37. {
  38. hana::pair<float, short*> p1(3.5f, 0);
  39. BOOST_HANA_RUNTIME_CHECK(hana::first(p1) == 3.5f);
  40. BOOST_HANA_RUNTIME_CHECK(hana::second(p1) == nullptr);
  41. // brace init
  42. hana::pair<float, short*> p2 = {3.5f, 0};
  43. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == 3.5f);
  44. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == nullptr);
  45. }
  46. {
  47. hana::pair<FromInt, int> p1(1, 2);
  48. BOOST_HANA_RUNTIME_CHECK(hana::first(p1) == FromInt(1));
  49. BOOST_HANA_RUNTIME_CHECK(hana::second(p1) == 2);
  50. // brace init
  51. hana::pair<FromInt, int> p2 = {1, 2};
  52. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == FromInt(1));
  53. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 2);
  54. }
  55. // Make sure the above works constexpr too
  56. {
  57. constexpr hana::pair<float, short*> p1(3.5f, 0);
  58. static_assert(hana::first(p1) == 3.5f, "");
  59. static_assert(hana::second(p1) == nullptr, "");
  60. // brace init
  61. constexpr hana::pair<float, short*> p2 = {3.5f, 0};
  62. static_assert(hana::first(p2) == 3.5f, "");
  63. static_assert(hana::second(p2) == nullptr, "");
  64. }
  65. {
  66. constexpr hana::pair<FromInt, int> p1(1, 2);
  67. static_assert(hana::first(p1) == FromInt(1), "");
  68. static_assert(hana::second(p1) == 2, "");
  69. // brace init
  70. constexpr hana::pair<FromInt, int> p2 = {1, 2};
  71. static_assert(hana::first(p2) == FromInt(1), "");
  72. static_assert(hana::second(p2) == 2, "");
  73. }
  74. }