cnstr.default.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/bool.hpp>
  5. #include <boost/hana/fwd/hash.hpp>
  6. #include <boost/hana/set.hpp>
  7. #include <boost/hana/type.hpp>
  8. #include <type_traits>
  9. namespace hana = boost::hana;
  10. template <int i>
  11. struct NoDefault {
  12. NoDefault() = delete;
  13. NoDefault(NoDefault const&) = default;
  14. constexpr explicit NoDefault(int) { }
  15. };
  16. template <int i, int j>
  17. auto operator==(NoDefault<i> const&, NoDefault<j> const&) { return hana::bool_<i == j>{}; }
  18. template <int i, int j>
  19. auto operator!=(NoDefault<i> const&, NoDefault<j> const&) { return hana::bool_<i != j>{}; }
  20. template <int i>
  21. struct Default {
  22. Default() = default;
  23. Default(Default const&) = default;
  24. constexpr explicit Default(int) { }
  25. };
  26. template <int i, int j>
  27. auto operator==(Default<i> const&, Default<j> const&) { return hana::bool_<i == j>{}; }
  28. template <int i, int j>
  29. auto operator!=(Default<i> const&, Default<j> const&) { return hana::bool_<i != j>{}; }
  30. namespace boost { namespace hana {
  31. template <int i>
  32. struct hash_impl<NoDefault<i>> {
  33. static constexpr auto apply(NoDefault<i> const&)
  34. { return hana::type_c<NoDefault<i>>; };
  35. };
  36. template <int i>
  37. struct hash_impl<Default<i>> {
  38. static constexpr auto apply(Default<i> const&)
  39. { return hana::type_c<Default<i>>; };
  40. };
  41. }}
  42. int main() {
  43. {
  44. auto set0 = hana::make_set();
  45. static_assert(std::is_default_constructible<decltype(set0)>{}, "");
  46. auto set1 = hana::make_set(Default<0>(int{}));
  47. static_assert(std::is_default_constructible<decltype(set1)>{}, "");
  48. auto set2 = hana::make_set(Default<0>(int{}), Default<1>(int{}));
  49. static_assert(std::is_default_constructible<decltype(set2)>{}, "");
  50. auto set3 = hana::make_set(Default<0>(int{}), NoDefault<1>(int{}), Default<2>(int{}));
  51. static_assert(!std::is_default_constructible<decltype(set3)>{}, "");
  52. }
  53. {
  54. auto set1 = hana::make_set(NoDefault<0>(int{}));
  55. static_assert(!std::is_default_constructible<decltype(set1)>{}, "");
  56. auto set2 = hana::make_set(NoDefault<0>(int{}), NoDefault<1>(int{}));
  57. static_assert(!std::is_default_constructible<decltype(set2)>{}, "");
  58. auto set3 = hana::make_set(NoDefault<0>(int{}), NoDefault<1>(int{}), NoDefault<2>(int{}));
  59. static_assert(!std::is_default_constructible<decltype(set3)>{}, "");
  60. }
  61. }