]>
Commit | Line | Data |
---|---|---|
fbd26352 | 1 | // Copyright (C) 2018-2019 Free Software Foundation, Inc. |
4e6aac30 | 2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++2a" } | |
19 | // { dg-do run { target c++2a } } | |
20 | ||
21 | #include <numeric> | |
22 | #include <iterator> | |
23 | #include <testsuite_hooks.h> | |
24 | ||
25 | struct Int | |
26 | { | |
27 | Int(int v) : val(v) { } | |
28 | ||
29 | ~Int() = default; | |
30 | ||
31 | Int(const Int& x) : val(x.val), copies(x.copies), moved_from(x.moved_from) | |
32 | { ++copies; } | |
33 | ||
34 | Int(Int&& x) : val(x.val), copies(x.copies), moved_from(x.moved_from) | |
35 | { x.moved_from = true; } | |
36 | ||
37 | Int& operator=(const Int& x) | |
38 | { | |
39 | val = x.val; | |
40 | copies = x.copies + 1; | |
41 | moved_from = x.moved_from; | |
42 | return *this; | |
43 | } | |
44 | ||
45 | Int& operator=(Int&& x) | |
46 | { | |
47 | val = x.val; | |
48 | copies = x.copies; | |
49 | moved_from = x.moved_from; | |
50 | x.moved_from = true; | |
51 | return *this; | |
52 | } | |
53 | ||
54 | int val = 0; | |
55 | int copies = 0; | |
56 | bool moved_from = false; | |
57 | }; | |
58 | ||
59 | Int operator+(Int x, Int y) { x.val += y.val; return x; } | |
60 | ||
61 | struct Add | |
62 | { | |
63 | Int operator()(Int x, Int y) const { x.val += y.val; return x; } | |
64 | }; | |
65 | ||
66 | void | |
67 | test01() | |
68 | { | |
69 | Int i[] = { 0, 1, 2, 3, 4 }; | |
70 | Int res = std::accumulate(std::begin(i), std::end(i), Int{0}); | |
71 | VERIFY( res.copies == 0 ); | |
72 | VERIFY( !res.moved_from ); | |
73 | for (const auto& r : i) | |
74 | VERIFY( !r.moved_from ); | |
75 | } | |
76 | ||
77 | void | |
78 | test02() | |
79 | { | |
80 | Int i[] = { 0, 1, 2, 3, 4 }; | |
81 | Int res = std::accumulate(std::begin(i), std::end(i), Int{0}, Add{}); | |
82 | VERIFY( res.copies == 0 ); | |
83 | VERIFY( !res.moved_from ); | |
84 | for (const auto& r : i) | |
85 | VERIFY( !r.moved_from ); | |
86 | } | |
87 | ||
88 | int | |
89 | main() | |
90 | { | |
91 | test01(); | |
92 | test02(); | |
93 | } |