alloc.pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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
  10. // <tuple>
  11. // template <class... Types> class tuple;
  12. // template <class Alloc>
  13. // tuple(allocator_arg_t, const Alloc& a);
  14. // NOTE: this constructor does not currently support tags derived from
  15. // allocator_arg_t because libc++ has to deduce the parameter as a template
  16. // argument. See PR27684 (https://bugs.llvm.org/show_bug.cgi?id=27684)
  17. #include <tuple>
  18. #include <cassert>
  19. #include "DefaultOnly.h"
  20. #include "allocators.h"
  21. #include "../alloc_first.h"
  22. #include "../alloc_last.h"
  23. template <class T = void>
  24. struct NonDefaultConstructible {
  25. constexpr NonDefaultConstructible() {
  26. static_assert(!std::is_same<T, T>::value, "Default Ctor instantiated");
  27. }
  28. explicit constexpr NonDefaultConstructible(int) {}
  29. };
  30. struct DerivedFromAllocArgT : std::allocator_arg_t {};
  31. int main()
  32. {
  33. {
  34. std::tuple<> t(std::allocator_arg, A1<int>());
  35. }
  36. {
  37. std::tuple<int> t(std::allocator_arg, A1<int>());
  38. assert(std::get<0>(t) == 0);
  39. }
  40. {
  41. std::tuple<DefaultOnly> t(std::allocator_arg, A1<int>());
  42. assert(std::get<0>(t) == DefaultOnly());
  43. }
  44. {
  45. assert(!alloc_first::allocator_constructed);
  46. std::tuple<alloc_first> t(std::allocator_arg, A1<int>(5));
  47. assert(alloc_first::allocator_constructed);
  48. assert(std::get<0>(t) == alloc_first());
  49. }
  50. {
  51. assert(!alloc_last::allocator_constructed);
  52. std::tuple<alloc_last> t(std::allocator_arg, A1<int>(5));
  53. assert(alloc_last::allocator_constructed);
  54. assert(std::get<0>(t) == alloc_last());
  55. }
  56. {
  57. alloc_first::allocator_constructed = false;
  58. std::tuple<DefaultOnly, alloc_first> t(std::allocator_arg, A1<int>(5));
  59. assert(std::get<0>(t) == DefaultOnly());
  60. assert(alloc_first::allocator_constructed);
  61. assert(std::get<1>(t) == alloc_first());
  62. }
  63. {
  64. alloc_first::allocator_constructed = false;
  65. alloc_last::allocator_constructed = false;
  66. std::tuple<DefaultOnly, alloc_first, alloc_last> t(std::allocator_arg,
  67. A1<int>(5));
  68. assert(std::get<0>(t) == DefaultOnly());
  69. assert(alloc_first::allocator_constructed);
  70. assert(std::get<1>(t) == alloc_first());
  71. assert(alloc_last::allocator_constructed);
  72. assert(std::get<2>(t) == alloc_last());
  73. }
  74. {
  75. alloc_first::allocator_constructed = false;
  76. alloc_last::allocator_constructed = false;
  77. std::tuple<DefaultOnly, alloc_first, alloc_last> t(std::allocator_arg,
  78. A2<int>(5));
  79. assert(std::get<0>(t) == DefaultOnly());
  80. assert(!alloc_first::allocator_constructed);
  81. assert(std::get<1>(t) == alloc_first());
  82. assert(!alloc_last::allocator_constructed);
  83. assert(std::get<2>(t) == alloc_last());
  84. }
  85. {
  86. // Test that the uses-allocator default constructor does not evaluate
  87. // its SFINAE when it otherwise shouldn't be selected. Do this by
  88. // using 'NonDefaultConstructible' which will cause a compile error
  89. // if std::is_default_constructible is evaluated on it.
  90. using T = NonDefaultConstructible<>;
  91. T v(42);
  92. std::tuple<T, T> t(v, v);
  93. (void)t;
  94. std::tuple<T, T> t2(42, 42);
  95. (void)t2;
  96. }
  97. }