cnstr.variadic_copy.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <string>
  7. namespace hana = boost::hana;
  8. template <typename ...>
  9. struct never { static constexpr bool value = false; };
  10. struct NoValueCtor {
  11. NoValueCtor() : id(++count) {}
  12. NoValueCtor(NoValueCtor const & other) : id(other.id) { ++count; }
  13. // The constexpr is required to make is_constructible instantiate this
  14. // template. The explicit is needed to test-around a similar bug with
  15. // is_convertible.
  16. template <typename T>
  17. constexpr explicit NoValueCtor(T)
  18. { static_assert(never<T>::value, "This should not be instantiated"); }
  19. static int count;
  20. int id;
  21. };
  22. int NoValueCtor::count = 0;
  23. struct NoValueCtorEmpty {
  24. NoValueCtorEmpty() {}
  25. NoValueCtorEmpty(NoValueCtorEmpty const &) {}
  26. template <typename T>
  27. constexpr explicit NoValueCtorEmpty(T)
  28. { static_assert(never<T>::value, "This should not be instantiated"); }
  29. };
  30. int main() {
  31. {
  32. hana::tuple<int> t(2);
  33. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2);
  34. }
  35. {
  36. constexpr hana::tuple<int> t(2);
  37. static_assert(hana::at_c<0>(t) == 2, "");
  38. }
  39. {
  40. constexpr hana::tuple<int> t;
  41. static_assert(hana::at_c<0>(t) == 0, "");
  42. }
  43. {
  44. constexpr hana::tuple<int, char*> t(2, nullptr);
  45. static_assert(hana::at_c<0>(t) == 2, "");
  46. static_assert(hana::at_c<1>(t) == nullptr, "");
  47. }
  48. {
  49. hana::tuple<int, char*> t(2, nullptr);
  50. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2);
  51. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == nullptr);
  52. }
  53. {
  54. hana::tuple<int, char*, std::string> t(2, nullptr, "text");
  55. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 2);
  56. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t) == nullptr);
  57. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == "text");
  58. }
  59. {
  60. hana::tuple<int, NoValueCtor, int, int> t(1, NoValueCtor(), 2, 3);
  61. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 1);
  62. BOOST_HANA_RUNTIME_CHECK(hana::at_c<1>(t).id == 1);
  63. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 2);
  64. BOOST_HANA_RUNTIME_CHECK(hana::at_c<3>(t) == 3);
  65. }
  66. {
  67. hana::tuple<int, NoValueCtorEmpty, int, int> t(1, NoValueCtorEmpty(), 2, 3);
  68. BOOST_HANA_RUNTIME_CHECK(hana::at_c<0>(t) == 1);
  69. BOOST_HANA_RUNTIME_CHECK(hana::at_c<2>(t) == 2);
  70. BOOST_HANA_RUNTIME_CHECK(hana::at_c<3>(t) == 3);
  71. }
  72. {
  73. struct T { };
  74. struct U { };
  75. struct V { };
  76. constexpr T t{};
  77. constexpr U u{};
  78. constexpr V v{};
  79. constexpr hana::tuple<T> x1{t}; (void)x1;
  80. constexpr hana::tuple<T, U> x2{t, u}; (void)x2;
  81. constexpr hana::tuple<T, U, V> x3{t, u, v}; (void)x3;
  82. }
  83. {
  84. struct T { };
  85. struct U { };
  86. struct V { };
  87. // Check for SFINAE-friendliness
  88. static_assert(!std::is_constructible<
  89. hana::tuple<T, U>, T
  90. >{}, "");
  91. static_assert(!std::is_constructible<
  92. hana::tuple<T, U>, U, T
  93. >{}, "");
  94. static_assert(!std::is_constructible<
  95. hana::tuple<T, U>, T, U, V
  96. >{}, "");
  97. }
  98. // Make sure we can initialize elements with the brace-init syntax.
  99. {
  100. struct Member { };
  101. struct Element { Member member; };
  102. hana::tuple<Element, Element> xs{{Member()}, {Member()}};
  103. hana::tuple<Element, Element> ys = {{Member()}, {Member()}};
  104. (void)xs; (void)ys;
  105. }
  106. }