cnstr.variadic_forward.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/tuple.hpp>
  6. #include <type_traits>
  7. namespace hana = boost::hana;
  8. struct MoveOnly {
  9. int data_;
  10. MoveOnly(MoveOnly const&) = delete;
  11. MoveOnly& operator=(MoveOnly const&) = delete;
  12. MoveOnly(int data = 1) : data_(data) { }
  13. MoveOnly(MoveOnly&& x) : data_(x.data_) { x.data_ = 0; }
  14. MoveOnly& operator=(MoveOnly&& x)
  15. { data_ = x.data_; x.data_ = 0; return *this; }
  16. int get() const {return data_;}
  17. bool operator==(const MoveOnly& x) const { return data_ == x.data_; }
  18. bool operator< (const MoveOnly& x) const { return data_ < x.data_; }
  19. };
  20. struct Empty { };
  21. struct A {
  22. int id_;
  23. explicit constexpr A(int i) : id_(i) {}
  24. };
  25. struct NoDefault { NoDefault() = delete; };
  26. int main() {
  27. {
  28. hana::tuple<MoveOnly> t(MoveOnly(0));
  29. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  30. }
  31. {
  32. hana::tuple<MoveOnly, MoveOnly> t(MoveOnly(0), MoveOnly(1));
  33. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  34. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1);
  35. }
  36. {
  37. hana::tuple<MoveOnly, MoveOnly, MoveOnly> t(
  38. MoveOnly(0), MoveOnly(1), MoveOnly(2)
  39. );
  40. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 0);
  41. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == 1);
  42. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 2);
  43. }
  44. {
  45. constexpr hana::tuple<Empty> t0{Empty()}; (void)t0;
  46. }
  47. {
  48. constexpr hana::tuple<A, A> t(3, 2);
  49. static_assert(hana::at_c<0>(t).id_ == 3, "");
  50. }
  51. {
  52. typedef hana::tuple<MoveOnly, NoDefault> Tuple;
  53. static_assert(!std::is_constructible<
  54. Tuple,
  55. MoveOnly
  56. >::value, "");
  57. static_assert(std::is_constructible<
  58. Tuple,
  59. MoveOnly, NoDefault
  60. >::value, "");
  61. }
  62. {
  63. typedef hana::tuple<MoveOnly, MoveOnly, NoDefault> Tuple;
  64. static_assert(!std::is_constructible<
  65. Tuple,
  66. MoveOnly, MoveOnly
  67. >::value, "");
  68. static_assert(std::is_constructible<
  69. Tuple,
  70. MoveOnly, MoveOnly, NoDefault
  71. >::value, "");
  72. }
  73. {
  74. typedef hana::tuple<MoveOnly, NoDefault> Tuple;
  75. typedef hana::tuple<MoveOnly, Tuple, MoveOnly, MoveOnly> NestedTuple;
  76. static_assert(!std::is_constructible<
  77. NestedTuple,
  78. MoveOnly, MoveOnly, MoveOnly, MoveOnly
  79. >::value, "");
  80. static_assert(std::is_constructible<
  81. NestedTuple,
  82. MoveOnly, Tuple, MoveOnly, MoveOnly
  83. >::value, "");
  84. }
  85. {
  86. typedef hana::tuple<MoveOnly, int> Tuple;
  87. typedef hana::tuple<MoveOnly, Tuple, MoveOnly, MoveOnly> NestedTuple;
  88. static_assert(!std::is_constructible<
  89. NestedTuple,
  90. MoveOnly, MoveOnly, MoveOnly, MoveOnly
  91. >::value, "");
  92. static_assert(std::is_constructible<
  93. NestedTuple,
  94. MoveOnly, Tuple, MoveOnly, MoveOnly
  95. >::value, "");
  96. }
  97. }