cnstr.copy.cpp 3.5 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/bool.hpp>
  6. #include <boost/hana/equal.hpp>
  7. #include <boost/hana/fwd/hash.hpp>
  8. #include <boost/hana/integral_constant.hpp>
  9. #include <boost/hana/map.hpp>
  10. #include <boost/hana/type.hpp>
  11. #include <string>
  12. #include <type_traits>
  13. namespace hana = boost::hana;
  14. struct NoCopy {
  15. NoCopy() = default;
  16. NoCopy(NoCopy const&) = delete;
  17. friend auto operator==(NoCopy const&, NoCopy const&) { return hana::true_c; }
  18. friend auto operator!=(NoCopy const&, NoCopy const&) { return hana::false_c; }
  19. };
  20. // Note: It is also useful to check with a non-empty class, because that
  21. // triggers different instantiations due to EBO.
  22. struct NoCopy_nonempty {
  23. NoCopy_nonempty() = default;
  24. NoCopy_nonempty(NoCopy_nonempty const&) = delete;
  25. int i;
  26. friend auto operator==(NoCopy_nonempty const&, NoCopy_nonempty const&) { return hana::true_c; }
  27. friend auto operator!=(NoCopy_nonempty const&, NoCopy_nonempty const&) { return hana::false_c; }
  28. };
  29. namespace boost { namespace hana {
  30. template <>
  31. struct hash_impl<NoCopy> {
  32. static constexpr auto apply(NoCopy const&)
  33. { return hana::type_c<NoCopy>; };
  34. };
  35. template <>
  36. struct hash_impl<NoCopy_nonempty> {
  37. static constexpr auto apply(NoCopy_nonempty const&)
  38. { return hana::type_c<NoCopy_nonempty>; };
  39. };
  40. }}
  41. int main() {
  42. {
  43. auto t0 = hana::make_map();
  44. auto t_implicit = t0;
  45. auto t_explicit(t0);
  46. (void)t_explicit;
  47. (void)t_implicit;
  48. }
  49. {
  50. auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, hana::int_c<20>));
  51. auto t_implicit = t0;
  52. auto t_explicit(t0);
  53. (void)t_implicit;
  54. (void)t_explicit;
  55. }
  56. {
  57. auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, hana::int_c<20>),
  58. hana::make_pair(hana::int_c<3>, hana::int_c<30>));
  59. auto t_implicit = t0;
  60. auto t_explicit(t0);
  61. (void)t_implicit;
  62. (void)t_explicit;
  63. }
  64. {
  65. auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, hana::int_c<20>),
  66. hana::make_pair(hana::int_c<3>, hana::int_c<30>),
  67. hana::make_pair(hana::type_c<void>, hana::type_c<void*>));
  68. auto t_implicit = t0;
  69. auto t_explicit(t0);
  70. (void)t_implicit;
  71. (void)t_explicit;
  72. }
  73. {
  74. constexpr auto t0 = hana::make_map(
  75. hana::make_pair(hana::int_c<2>, hana::int_c<20>),
  76. hana::make_pair(hana::int_c<3>, hana::int_c<30>),
  77. hana::make_pair(hana::type_c<void>, hana::type_c<void*>));
  78. constexpr auto t_implicit = t0;
  79. constexpr auto t_explicit(t0);
  80. (void)t_implicit;
  81. (void)t_explicit;
  82. }
  83. {
  84. auto t0 = hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"}));
  85. auto copy = t0;
  86. BOOST_HANA_RUNTIME_CHECK(
  87. copy == hana::make_map(hana::make_pair(hana::int_c<2>, std::string{"abcdef"}))
  88. );
  89. }
  90. {
  91. using Map1 = hana::map<hana::pair<NoCopy, NoCopy>>;
  92. Map1 map1; (void)map1;
  93. static_assert(!std::is_copy_constructible<Map1>::value, "");
  94. using Map2 = hana::map<hana::pair<NoCopy_nonempty, NoCopy_nonempty>>;
  95. Map2 map2; (void)map2;
  96. static_assert(!std::is_copy_constructible<Map2>::value, "");
  97. }
  98. }