deduct.pass.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // <queue>
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // UNSUPPORTED: clang-5, apple-clang-9
  11. // UNSUPPORTED: libcpp-no-deduction-guides
  12. // Clang 5 will generate bad implicit deduction guides
  13. // Specifically, for the copy constructor.
  14. // template<class Container>
  15. // queue(Container) -> queue<typename Container::value_type, Container>;
  16. //
  17. // template<class Container, class Allocator>
  18. // queue(Container, Allocator) -> queue<typename Container::value_type, Container>;
  19. #include <queue>
  20. #include <list>
  21. #include <iterator>
  22. #include <cassert>
  23. #include <cstddef>
  24. #include <climits> // INT_MAX
  25. #include "test_macros.h"
  26. #include "test_iterators.h"
  27. #include "test_allocator.h"
  28. struct A {};
  29. int main(int, char**)
  30. {
  31. // Test the explicit deduction guides
  32. {
  33. std::list<int> l{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  34. std::queue que(l);
  35. static_assert(std::is_same_v<decltype(que), std::queue<int, std::list<int>>>, "");
  36. assert(que.size() == l.size());
  37. assert(que.back() == l.back());
  38. }
  39. {
  40. std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
  41. std::queue que(l, test_allocator<long>(0,2)); // different allocator
  42. static_assert(std::is_same_v<decltype(que)::container_type, std::list<long, test_allocator<long>>>, "");
  43. static_assert(std::is_same_v<decltype(que)::value_type, long>, "");
  44. assert(que.size() == 10);
  45. assert(que.back() == 19);
  46. // I'd like to assert that we've gotten the right allocator in the queue, but
  47. // I don't know how to get at the underlying container.
  48. }
  49. // Test the implicit deduction guides
  50. {
  51. // We don't expect this one to work - no way to implicitly get value_type
  52. // std::queue que(std::allocator<int>()); // queue (allocator &)
  53. }
  54. {
  55. std::queue<A> source;
  56. std::queue que(source); // queue(queue &)
  57. static_assert(std::is_same_v<decltype(que)::value_type, A>, "");
  58. static_assert(std::is_same_v<decltype(que)::container_type, std::deque<A>>, "");
  59. assert(que.size() == 0);
  60. }
  61. {
  62. // This one is odd - you can pass an allocator in to use, but the allocator
  63. // has to match the type of the one used by the underlying container
  64. typedef short T;
  65. typedef test_allocator<T> A;
  66. typedef std::deque<T, A> C;
  67. C c{0,1,2,3};
  68. std::queue<T, C> source(c);
  69. std::queue que(source, A(2)); // queue(queue &, allocator)
  70. static_assert(std::is_same_v<decltype(que)::value_type, T>, "");
  71. static_assert(std::is_same_v<decltype(que)::container_type, C>, "");
  72. assert(que.size() == 4);
  73. assert(que.back() == 3);
  74. }
  75. return 0;
  76. }