cnstr.copy.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <type_traits>
  9. namespace hana = boost::hana;
  10. template <typename Target>
  11. struct implicit_to {
  12. constexpr operator Target() const { return Target{}; }
  13. };
  14. struct NoCopy {
  15. NoCopy() = default;
  16. NoCopy(NoCopy const&) = delete;
  17. };
  18. // Note: It is also useful to check with a non-empty class, because that
  19. // triggers different instantiations due to EBO.
  20. struct NoCopy_nonempty {
  21. NoCopy_nonempty() = default;
  22. NoCopy_nonempty(NoCopy_nonempty const&) = delete;
  23. int i;
  24. };
  25. int main() {
  26. {
  27. typedef std::pair<int, short> P1;
  28. hana::pair<int, short> p1(3, 4);
  29. hana::pair<int, short> p2 = p1;
  30. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == 3);
  31. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4);
  32. }
  33. static_assert(std::is_trivially_copy_constructible<hana::pair<int, int>>{}, "");
  34. // make sure it also works constexpr
  35. {
  36. constexpr hana::pair<int, short> p1(3, 4);
  37. constexpr hana::pair<int, short> p2 = p1;
  38. static_assert(hana::first(p2) == 3, "");
  39. static_assert(hana::second(p2) == 4, "");
  40. }
  41. // Make sure it works across pair types (pair<T, U> -> pair<U, V>)
  42. {
  43. hana::pair<int, short> p1(3, 4);
  44. hana::pair<double, long> p2 = p1;
  45. BOOST_HANA_RUNTIME_CHECK(hana::first(p2) == 3);
  46. BOOST_HANA_RUNTIME_CHECK(hana::second(p2) == 4);
  47. }
  48. {
  49. struct target1 { };
  50. struct target2 { };
  51. using Target = hana::pair<target1, target2>;
  52. auto p1_ = hana::make_pair(target1{}, target2{});
  53. Target p1(p1_); (void)p1;
  54. auto p2_ = hana::make_pair(implicit_to<target1>{}, target2{});
  55. Target p2(p2_); (void)p2;
  56. auto p3_ = hana::make_pair(target1{}, implicit_to<target2>{});
  57. Target p3(p3_); (void)p3;
  58. auto p4_ = hana::make_pair(implicit_to<target1>{}, implicit_to<target2>{});
  59. Target p4(p4_); (void)p4;
  60. }
  61. // And also constexpr across pair types
  62. {
  63. constexpr hana::pair<int, short> p1(3, 4);
  64. constexpr hana::pair<double, long> p2 = p1;
  65. static_assert(hana::first(p2) == 3, "");
  66. static_assert(hana::second(p2) == 4, "");
  67. }
  68. // Make sure we don't define the copy constructor when it shouldn't be defined.
  69. {
  70. using Pair1 = hana::pair<NoCopy, NoCopy>;
  71. Pair1 pair1; (void)pair1;
  72. static_assert(!std::is_copy_constructible<Pair1>::value, "");
  73. using Pair2 = hana::pair<NoCopy_nonempty, NoCopy_nonempty>;
  74. Pair2 pair2; (void)pair2;
  75. static_assert(!std::is_copy_constructible<Pair2>::value, "");
  76. }
  77. }