Debian Codegraph @

libstdc++-v3/testsuite/23_containers/vector/modifiers/emplace/92878_92947.cc

raw
1 // { dg-do run { target c++20 } }
2 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 //
4 // This file is part of the GNU ISO C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 3, or (at your option)
8 // any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this library; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
18
19 #include <vector>
20 #include <testsuite_hooks.h>
21
22 struct aggressive_aggregate
23 {
24 int a;
25 int b;
26 };
27
28 void test_emplace()
29 {
30 std::vector<aggressive_aggregate> x{{42, 666}};
31 auto y = x.emplace(x.begin(), 1, 2);
32 VERIFY(y->a == 1);
33 VERIFY(y->b == 2);
34 y = x.emplace(x.begin(), 1);
35 VERIFY(y->a == 1);
36 VERIFY(y->b == 0);
37 y = x.emplace(x.begin());
38 VERIFY(y->a == 0);
39 VERIFY(y->b == 0);
40 }
41
42 void test_emplace_back()
43 {
44 std::vector<aggressive_aggregate> x;
45 x.emplace_back(1, 2);
46 VERIFY(x.back().a == 1);
47 VERIFY(x.back().b == 2);
48 x.emplace_back(1);
49 VERIFY(x.back().a == 1);
50 VERIFY(x.back().b == 0);
51 x.emplace_back();
52 VERIFY(x.back().a == 0);
53 VERIFY(x.back().b == 0);
54 }
55
56 int main()
57 {
58 test_emplace();
59 test_emplace_back();
60 }