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. (void)t;
  42. }
  43. {
  44. std::tuple<int> t;
  45. assert(std::get<0>(t) == 0);
  46. }
  47. {
  48. std::tuple<int, char*> t;
  49. assert(std::get<0>(t) == 0);
  50. assert(std::get<1>(t) == nullptr);
  51. }
  52. {
  53. std::tuple<int, char*, std::string> t;
  54. assert(std::get<0>(t) == 0);
  55. assert(std::get<1>(t) == nullptr);
  56. assert(std::get<2>(t) == "");
  57. }
  58. {
  59. std::tuple<int, char*, std::string, DefaultOnly> t;
  60. assert(std::get<0>(t) == 0);
  61. assert(std::get<1>(t) == nullptr);
  62. assert(std::get<2>(t) == "");
  63. assert(std::get<3>(t) == DefaultOnly());
  64. }
  65. {
  66. // See bug #21157.
  67. static_assert(!std::is_default_constructible<std::tuple<NoDefault>>(), "");
  68. static_assert(!std::is_default_constructible<std::tuple<DefaultOnly, NoDefault>>(), "");
  69. static_assert(!std::is_default_constructible<std::tuple<NoDefault, DefaultOnly, NoDefault>>(), "");
  70. }
  71. {
  72. static_assert(noexcept(std::tuple<NoExceptDefault>()), "");
  73. static_assert(noexcept(std::tuple<NoExceptDefault, NoExceptDefault>()), "");
  74. static_assert(!noexcept(std::tuple<ThrowingDefault, NoExceptDefault>()), "");
  75. static_assert(!noexcept(std::tuple<NoExceptDefault, ThrowingDefault>()), "");
  76. static_assert(!noexcept(std::tuple<ThrowingDefault, ThrowingDefault>()), "");
  77. }
  78. {
  79. constexpr std::tuple<> t;
  80. (void)t;
  81. }
  82. {
  83. constexpr std::tuple<int> t;
  84. assert(std::get<0>(t) == 0);
  85. }
  86. {
  87. constexpr std::tuple<int, char*> t;
  88. assert(std::get<0>(t) == 0);
  89. assert(std::get<1>(t) == nullptr);
  90. }
  91. {
  92. // Check that the SFINAE on the default constructor is not evaluated when
  93. // it isn't needed. If the default constructor is evaluated then this test
  94. // should fail to compile.
  95. IllFormedDefault v(0);
  96. std::tuple<IllFormedDefault> t(v);
  97. }
  98. }