default.pass.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <tuple>
  10. // template <class... Types> class tuple;
  11. // constexpr tuple();
  12. // UNSUPPORTED: c++98, c++03
  13. #include <tuple>
  14. #include <string>
  15. #include <cassert>
  16. #include <type_traits>
  17. #include "DefaultOnly.h"
  18. struct NoDefault {
  19. NoDefault() = delete;
  20. explicit NoDefault(int) { }
  21. };
  22. struct NoExceptDefault {
  23. NoExceptDefault() noexcept = default;
  24. };
  25. struct ThrowingDefault {
  26. ThrowingDefault() { }
  27. };
  28. struct IllFormedDefault {
  29. IllFormedDefault(int x) : value(x) {}
  30. template <bool Pred = false>
  31. constexpr IllFormedDefault() {
  32. static_assert(Pred,
  33. "The default constructor should not be instantiated");
  34. }
  35. int value;
  36. };
  37. int main()
  38. {
  39. {
  40. std::tuple<> t;
  41. }
  42. {
  43. std::tuple<int> t;
  44. assert(std::get<0>(t) == 0);
  45. }
  46. {
  47. std::tuple<int, char*> t;
  48. assert(std::get<0>(t) == 0);
  49. assert(std::get<1>(t) == nullptr);
  50. }
  51. {
  52. std::tuple<int, char*, std::string> t;
  53. assert(std::get<0>(t) == 0);
  54. assert(std::get<1>(t) == nullptr);
  55. assert(std::get<2>(t) == "");
  56. }
  57. {
  58. std::tuple<int, char*, std::string, DefaultOnly> t;
  59. assert(std::get<0>(t) == 0);
  60. assert(std::get<1>(t) == nullptr);
  61. assert(std::get<2>(t) == "");
  62. assert(std::get<3>(t) == DefaultOnly());
  63. }
  64. {
  65. // See bug #21157.
  66. static_assert(!std::is_default_constructible<std::tuple<NoDefault>>(), "");
  67. static_assert(!std::is_default_constructible<std::tuple<DefaultOnly, NoDefault>>(), "");
  68. static_assert(!std::is_default_constructible<std::tuple<NoDefault, DefaultOnly, NoDefault>>(), "");
  69. }
  70. {
  71. static_assert(noexcept(std::tuple<NoExceptDefault>()), "");
  72. static_assert(noexcept(std::tuple<NoExceptDefault, NoExceptDefault>()), "");
  73. static_assert(!noexcept(std::tuple<ThrowingDefault, NoExceptDefault>()), "");
  74. static_assert(!noexcept(std::tuple<NoExceptDefault, ThrowingDefault>()), "");
  75. static_assert(!noexcept(std::tuple<ThrowingDefault, ThrowingDefault>()), "");
  76. }
  77. #ifndef _LIBCPP_HAS_NO_CONSTEXPR
  78. {
  79. constexpr std::tuple<> t;
  80. }
  81. {
  82. constexpr std::tuple<int> t;
  83. assert(std::get<0>(t) == 0);
  84. }
  85. {
  86. constexpr std::tuple<int, char*> t;
  87. assert(std::get<0>(t) == 0);
  88. assert(std::get<1>(t) == nullptr);
  89. }
  90. {
  91. // Check that the SFINAE on the default constructor is not evaluated when
  92. // it isn't needed. If the default constructor is evaluated then this test
  93. // should fail to compile.
  94. IllFormedDefault v(0);
  95. std::tuple<IllFormedDefault> t(v);
  96. }
  97. #endif
  98. }