deduction.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright (C) 2019-2022 Free Software Foundation, Inc.
  2. //
  3. // This file is part of the GNU ISO C++ Library. This library is free
  4. // software; you can redistribute it and/or modify it under the
  5. // terms of the GNU General Public License as published by the
  6. // Free Software Foundation; either version 3, or (at your option)
  7. // any later version.
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. // You should have received a copy of the GNU General Public License along
  13. // with this library; see the file COPYING3. If not see
  14. // <http://www.gnu.org/licenses/>.
  15. // { dg-do compile { target c++17 } }
  16. #include <queue>
  17. #include <deque>
  18. #include <list>
  19. #include <testsuite_allocator.h>
  20. template<typename T, typename U> struct require_same;
  21. template<typename T> struct require_same<T, T> { using type = void; };
  22. template<typename T, typename U>
  23. typename require_same<T, U>::type
  24. check_type(U&) { }
  25. void
  26. test01()
  27. {
  28. std::queue<unsigned> s0;
  29. std::queue s1 = s0;
  30. check_type<std::queue<unsigned>>(s1);
  31. std::queue s2 = std::move(s0);
  32. check_type<std::queue<unsigned>>(s2);
  33. const std::queue s3 = s0;
  34. check_type<const std::queue<unsigned>>(s3);
  35. const std::queue s4 = s3;
  36. check_type<const std::queue<unsigned>>(s4);
  37. std::allocator<unsigned> a;
  38. std::queue s5(s0, a);
  39. check_type<std::queue<unsigned>>(s5);
  40. std::queue s6(std::move(s0), a);
  41. check_type<std::queue<unsigned>>(s6);
  42. const std::queue s7(s3, a);
  43. check_type<const std::queue<unsigned>>(s7);
  44. }
  45. void
  46. test02()
  47. {
  48. std::deque<unsigned> d;
  49. std::list<long> l;
  50. std::queue s1(d);
  51. check_type<std::queue<unsigned>>(s1);
  52. std::queue s2(d, d.get_allocator());
  53. check_type<std::queue<unsigned>>(s2);
  54. std::queue s3(std::move(d));
  55. check_type<std::queue<unsigned>>(s3);
  56. std::queue s4(std::move(d), d.get_allocator());
  57. check_type<std::queue<unsigned>>(s4);
  58. std::queue s5(l);
  59. check_type<std::queue<long, std::list<long>>>(s5);
  60. std::queue s6(l, l.get_allocator());
  61. check_type<std::queue<long, std::list<long>>>(s6);
  62. std::queue s7(std::move(l));
  63. check_type<std::queue<long, std::list<long>>>(s7);
  64. std::queue s8(std::move(l), l.get_allocator());
  65. check_type<std::queue<long, std::list<long>>>(s8);
  66. }
  67. struct Pool;
  68. template<typename T>
  69. struct Alloc : __gnu_test::SimpleAllocator<T>
  70. {
  71. Alloc(Pool*) { }
  72. template<typename U>
  73. Alloc(const Alloc<U>&) { }
  74. };
  75. void
  76. test_p1518r2()
  77. {
  78. // P1518R2 - Stop overconstraining allocators in container deduction guides.
  79. // This is a C++23 feature but we support it for C++17 too.
  80. using Deque = std::deque<unsigned, Alloc<unsigned>>;
  81. using List = std::list<long, Alloc<long>>;
  82. Pool* p = nullptr;
  83. Deque d(p);
  84. List l(p);
  85. std::queue q1(d, p);
  86. check_type<std::queue<unsigned, Deque>>(q1);
  87. std::queue q2(l, p);
  88. check_type<std::queue<long, List>>(q2);
  89. std::queue q3(q2, p);
  90. check_type<std::queue<long, List>>(q3);
  91. }