implicit_deduction_guides.pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // UNSUPPORTED: libcpp-no-deduction-guides
  11. // GCC's implementation of class template deduction is still immature and runs
  12. // into issues with libc++. However GCC accepts this code when compiling
  13. // against libstdc++.
  14. // XFAIL: gcc
  15. // <tuple>
  16. // Test that the constructors offered by std::tuple are formulated
  17. // so they're compatible with implicit deduction guides, or if that's not
  18. // possible that they provide explicit guides to make it work.
  19. #include <tuple>
  20. #include <memory>
  21. #include <cassert>
  22. #include "test_macros.h"
  23. #include "archetypes.hpp"
  24. // Overloads
  25. // using A = Allocator
  26. // using AT = std::allocator_arg_t
  27. // ---------------
  28. // (1) tuple(const Types&...) -> tuple<Types...>
  29. // (2) explicit tuple(const Types&...) -> tuple<Types...>
  30. // (3) tuple(AT, A const&, Types const&...) -> tuple<Types...>
  31. // (4) explicit tuple(AT, A const&, Types const&...) -> tuple<Types...>
  32. // (5) tuple(tuple const& t) -> decltype(t)
  33. // (6) tuple(tuple&& t) -> decltype(t)
  34. // (7) tuple(AT, A const&, tuple const& t) -> decltype(t)
  35. // (8) tuple(AT, A const&, tuple&& t) -> decltype(t)
  36. void test_primary_template()
  37. {
  38. const std::allocator<int> A;
  39. const auto AT = std::allocator_arg;
  40. { // Testing (1)
  41. int x = 101;
  42. std::tuple t1(42);
  43. ASSERT_SAME_TYPE(decltype(t1), std::tuple<int>);
  44. std::tuple t2(x, 0.0, nullptr);
  45. ASSERT_SAME_TYPE(decltype(t2), std::tuple<int, double, decltype(nullptr)>);
  46. }
  47. { // Testing (2)
  48. using T = ExplicitTestTypes::TestType;
  49. static_assert(!std::is_convertible<T const&, T>::value, "");
  50. std::tuple t1(T{});
  51. ASSERT_SAME_TYPE(decltype(t1), std::tuple<T>);
  52. const T v{};
  53. std::tuple t2(T{}, 101l, v);
  54. ASSERT_SAME_TYPE(decltype(t2), std::tuple<T, long, T>);
  55. }
  56. { // Testing (3)
  57. int x = 101;
  58. std::tuple t1(AT, A, 42);
  59. ASSERT_SAME_TYPE(decltype(t1), std::tuple<int>);
  60. std::tuple t2(AT, A, 42, 0.0, x);
  61. ASSERT_SAME_TYPE(decltype(t2), std::tuple<int, double, int>);
  62. }
  63. { // Testing (4)
  64. using T = ExplicitTestTypes::TestType;
  65. static_assert(!std::is_convertible<T const&, T>::value, "");
  66. std::tuple t1(AT, A, T{});
  67. ASSERT_SAME_TYPE(decltype(t1), std::tuple<T>);
  68. const T v{};
  69. std::tuple t2(AT, A, T{}, 101l, v);
  70. ASSERT_SAME_TYPE(decltype(t2), std::tuple<T, long, T>);
  71. }
  72. { // Testing (5)
  73. using Tup = std::tuple<int, decltype(nullptr)>;
  74. const Tup t(42, nullptr);
  75. std::tuple t1(t);
  76. ASSERT_SAME_TYPE(decltype(t1), Tup);
  77. }
  78. { // Testing (6)
  79. using Tup = std::tuple<void*, unsigned, char>;
  80. std::tuple t1(Tup(nullptr, 42, 'a'));
  81. ASSERT_SAME_TYPE(decltype(t1), Tup);
  82. }
  83. { // Testing (7)
  84. using Tup = std::tuple<int, decltype(nullptr)>;
  85. const Tup t(42, nullptr);
  86. std::tuple t1(AT, A, t);
  87. ASSERT_SAME_TYPE(decltype(t1), Tup);
  88. }
  89. { // Testing (8)
  90. using Tup = std::tuple<void*, unsigned, char>;
  91. std::tuple t1(AT, A, Tup(nullptr, 42, 'a'));
  92. ASSERT_SAME_TYPE(decltype(t1), Tup);
  93. }
  94. }
  95. // Overloads
  96. // using A = Allocator
  97. // using AT = std::allocator_arg_t
  98. // ---------------
  99. // (1) tuple() -> tuple<>
  100. // (2) tuple(AT, A const&) -> tuple<>
  101. // (3) tuple(tuple const&) -> tuple<>
  102. // (4) tuple(tuple&&) -> tuple<>
  103. // (5) tuple(AT, A const&, tuple const&) -> tuple<>
  104. // (6) tuple(AT, A const&, tuple&&) -> tuple<>
  105. void test_empty_specialization()
  106. {
  107. std::allocator<int> A;
  108. const auto AT = std::allocator_arg;
  109. { // Testing (1)
  110. std::tuple t1{};
  111. ASSERT_SAME_TYPE(decltype(t1), std::tuple<>);
  112. }
  113. { // Testing (2)
  114. std::tuple t1{AT, A};
  115. ASSERT_SAME_TYPE(decltype(t1), std::tuple<>);
  116. }
  117. { // Testing (3)
  118. const std::tuple<> t{};
  119. std::tuple t1(t);
  120. ASSERT_SAME_TYPE(decltype(t1), std::tuple<>);
  121. }
  122. { // Testing (4)
  123. std::tuple t1(std::tuple<>{});
  124. ASSERT_SAME_TYPE(decltype(t1), std::tuple<>);
  125. }
  126. { // Testing (5)
  127. const std::tuple<> t{};
  128. std::tuple t1(AT, A, t);
  129. ASSERT_SAME_TYPE(decltype(t1), std::tuple<>);
  130. }
  131. { // Testing (6)
  132. std::tuple t1(AT, A, std::tuple<>{});
  133. ASSERT_SAME_TYPE(decltype(t1), std::tuple<>);
  134. }
  135. }
  136. int main() {
  137. test_primary_template();
  138. test_empty_specialization();
  139. }