cnstr.default.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. struct NoDefault {
  11. NoDefault() = delete;
  12. explicit constexpr NoDefault(int) { }
  13. };
  14. struct NoDefault_nonempty {
  15. NoDefault_nonempty() = delete;
  16. explicit constexpr NoDefault_nonempty(int k) : i(k) { }
  17. int i;
  18. };
  19. struct DefaultOnly {
  20. DefaultOnly() = default;
  21. DefaultOnly(DefaultOnly const&) = delete;
  22. DefaultOnly(DefaultOnly&&) = delete;
  23. };
  24. struct NonConstexprDefault {
  25. NonConstexprDefault() { }
  26. };
  27. int main() {
  28. {
  29. hana::pair<float, short*> p;
  30. BOOST_HANA_RUNTIME_CHECK(hana::first(p) == 0.0f);
  31. BOOST_HANA_RUNTIME_CHECK(hana::second(p) == nullptr);
  32. }
  33. // make sure it also works constexpr
  34. {
  35. constexpr hana::pair<float, short*> p;
  36. static_assert(hana::first(p) == 0.0f, "");
  37. static_assert(hana::second(p) == nullptr, "");
  38. }
  39. // make sure the default constructor is not instantiated when the
  40. // members of the pair are not default-constructible
  41. {
  42. using Pair1 = hana::pair<NoDefault, NoDefault>;
  43. Pair1 p1{NoDefault{1}, NoDefault{2}}; (void)p1;
  44. static_assert(!std::is_default_constructible<Pair1>{}, "");
  45. using Pair2 = hana::pair<NoDefault_nonempty, NoDefault_nonempty>;
  46. Pair2 p2{NoDefault_nonempty{1}, NoDefault_nonempty{2}}; (void)p2;
  47. static_assert(!std::is_default_constructible<Pair2>{}, "");
  48. }
  49. // make sure it works when only the default constructor is defined
  50. {
  51. hana::pair<DefaultOnly, DefaultOnly> p;
  52. (void)p;
  53. }
  54. {
  55. hana::pair<NonConstexprDefault, NonConstexprDefault> p;
  56. (void)p;
  57. }
  58. }