emplace.pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // UNSUPPORTED: c++98, c++03
  9. // <queue>
  10. // template <class... Args> decltype(auto) emplace(Args&&... args);
  11. // return type is 'decltype(auto)' in C++17; 'void' before
  12. // whatever the return type of the underlying container's emplace_back() returns.
  13. #include <queue>
  14. #include <cassert>
  15. #include <list>
  16. #include "test_macros.h"
  17. #include "../../../Emplaceable.h"
  18. template <typename Queue>
  19. void test_return_type() {
  20. typedef typename Queue::container_type Container;
  21. typedef typename Container::value_type value_type;
  22. typedef decltype(std::declval<Queue>().emplace(std::declval<value_type &>())) queue_return_type;
  23. #if TEST_STD_VER > 14
  24. typedef decltype(std::declval<Container>().emplace_back(std::declval<value_type>())) container_return_type;
  25. static_assert(std::is_same<queue_return_type, container_return_type>::value, "");
  26. #else
  27. static_assert(std::is_same<queue_return_type, void>::value, "");
  28. #endif
  29. }
  30. int main(int, char**)
  31. {
  32. test_return_type<std::queue<int> > ();
  33. test_return_type<std::queue<int, std::list<int> > > ();
  34. std::queue<Emplaceable> q;
  35. #if TEST_STD_VER > 14
  36. typedef Emplaceable T;
  37. T& r1 = q.emplace(1, 2.5);
  38. assert(&r1 == &q.back());
  39. T& r2 = q.emplace(2, 3.5);
  40. assert(&r2 == &q.back());
  41. T& r3 = q.emplace(3, 4.5);
  42. assert(&r3 == &q.back());
  43. assert(&r1 == &q.front());
  44. #else
  45. q.emplace(1, 2.5);
  46. q.emplace(2, 3.5);
  47. q.emplace(3, 4.5);
  48. #endif
  49. assert(q.size() == 3);
  50. assert(q.front() == Emplaceable(1, 2.5));
  51. assert(q.back() == Emplaceable(3, 4.5));
  52. return 0;
  53. }