deduct.pass.cpp 2.9 KB

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