default.pass.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <tuple>
  9. // template <class... Types> class tuple;
  10. // explicit(see-below) constexpr tuple();
  11. // UNSUPPORTED: c++98, c++03
  12. #include <tuple>
  13. #include <string>
  14. #include <cassert>
  15. #include <type_traits>
  16. #include "test_macros.h"
  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(int, char**)
  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. {
  99. struct Base { };
  100. struct Derived : Base { protected: Derived() = default; };
  101. static_assert(!std::is_default_constructible<std::tuple<Derived, int> >::value, "");
  102. }
  103. return 0;
  104. }