construct.pass.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <memory>
  10. // template <class OuterAlloc, class... InnerAllocs>
  11. // class scoped_allocator_adaptor
  12. // template <class T, class... Args> void construct(T* p, Args&&... args);
  13. #include <scoped_allocator>
  14. #include <cassert>
  15. #include <string>
  16. #include "../allocators.h"
  17. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  18. struct B
  19. {
  20. static bool constructed;
  21. typedef A1<B> allocator_type;
  22. explicit B(std::allocator_arg_t, const allocator_type& a, int i)
  23. {
  24. assert(a.id() == 5);
  25. assert(i == 6);
  26. constructed = true;
  27. }
  28. };
  29. bool B::constructed = false;
  30. struct C
  31. {
  32. static bool constructed;
  33. typedef std::scoped_allocator_adaptor<A2<C>> allocator_type;
  34. explicit C(std::allocator_arg_t, const allocator_type& a, int i)
  35. {
  36. assert(a.id() == 7);
  37. assert(i == 8);
  38. constructed = true;
  39. }
  40. };
  41. bool C::constructed = false;
  42. struct D
  43. {
  44. static bool constructed;
  45. typedef std::scoped_allocator_adaptor<A2<D>> allocator_type;
  46. explicit D(int i, int j, const allocator_type& a)
  47. {
  48. assert(i == 1);
  49. assert(j == 2);
  50. assert(a.id() == 3);
  51. constructed = true;
  52. }
  53. };
  54. bool D::constructed = false;
  55. struct E
  56. {
  57. static bool constructed;
  58. typedef std::scoped_allocator_adaptor<A1<E>> allocator_type;
  59. explicit E(int i, int j, const allocator_type& a)
  60. {
  61. assert(i == 1);
  62. assert(j == 2);
  63. assert(a.id() == 50);
  64. constructed = true;
  65. }
  66. };
  67. bool E::constructed = false;
  68. struct F
  69. {
  70. static bool constructed;
  71. typedef std::scoped_allocator_adaptor<A2<F>> allocator_type;
  72. explicit F(int i, int j)
  73. {
  74. assert(i == 1);
  75. assert(j == 2);
  76. }
  77. explicit F(int i, int j, const allocator_type& a)
  78. {
  79. assert(i == 1);
  80. assert(j == 2);
  81. assert(a.id() == 50);
  82. constructed = true;
  83. }
  84. };
  85. bool F::constructed = false;
  86. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  87. int main()
  88. {
  89. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  90. {
  91. typedef std::scoped_allocator_adaptor<A1<std::string>> A;
  92. A a;
  93. char buf[100];
  94. typedef std::string S;
  95. S* s = (S*)buf;
  96. a.construct(s, 4, 'c');
  97. assert(*s == "cccc");
  98. s->~S();
  99. }
  100. {
  101. typedef std::scoped_allocator_adaptor<A1<B>> A;
  102. A a(A1<B>(5));
  103. char buf[100];
  104. typedef B S;
  105. S* s = (S*)buf;
  106. a.construct(s, 6);
  107. assert(S::constructed);
  108. s->~S();
  109. }
  110. {
  111. typedef std::scoped_allocator_adaptor<A1<int>, A2<C>> A;
  112. A a(A1<int>(5), A2<C>(7));
  113. char buf[100];
  114. typedef C S;
  115. S* s = (S*)buf;
  116. a.construct(s, 8);
  117. assert(S::constructed);
  118. s->~S();
  119. }
  120. {
  121. typedef std::scoped_allocator_adaptor<A1<int>, A2<D>> A;
  122. A a(A1<int>(5), A2<D>(3));
  123. char buf[100];
  124. typedef D S;
  125. S* s = (S*)buf;
  126. a.construct(s, 1, 2);
  127. assert(S::constructed);
  128. s->~S();
  129. }
  130. {
  131. typedef std::scoped_allocator_adaptor<A3<E>, A2<E>> K;
  132. typedef std::scoped_allocator_adaptor<K, A1<E>> A;
  133. A a(K(), A1<E>(50));
  134. char buf[100];
  135. typedef E S;
  136. S* s = (S*)buf;
  137. A3<E>::constructed = false;
  138. a.construct(s, 1, 2);
  139. assert(S::constructed);
  140. assert(A3<E>::constructed);
  141. s->~S();
  142. }
  143. {
  144. typedef std::scoped_allocator_adaptor<A3<F>, A2<F>> K;
  145. typedef std::scoped_allocator_adaptor<K, A1<F>> A;
  146. A a(K(), A1<F>(50));
  147. char buf[100];
  148. typedef F S;
  149. S* s = (S*)buf;
  150. A3<F>::constructed = false;
  151. a.construct(s, 1, 2);
  152. assert(!S::constructed);
  153. assert(A3<F>::constructed);
  154. s->~S();
  155. }
  156. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  157. }