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