varasm.c (output_constant): Add new parameter merge_strings.
[official-gcc.git] / libstdc++-v3 / testsuite / 26_numerics / inner_product / lwg2055.cc
blob7fea5d094762b6808fb6a536af663e3b51015b28
1 // Copyright (C) 2018 Free Software Foundation, Inc.
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.
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
21 #include <numeric>
22 #include <iterator>
23 #include <testsuite_hooks.h>
25 struct Int
27 Int(int v) : val(v) { }
29 ~Int() = default;
31 Int(const Int& x) : val(x.val), copies(x.copies), moved_from(x.moved_from)
32 { ++copies; }
34 Int(Int&& x) : val(x.val), copies(x.copies), moved_from(x.moved_from)
35 { x.moved_from = true; }
37 Int& operator=(const Int& x)
39 val = x.val;
40 copies = x.copies + 1;
41 moved_from = x.moved_from;
42 return *this;
45 Int& operator=(Int&& x)
47 val = x.val;
48 copies = x.copies;
49 moved_from = x.moved_from;
50 x.moved_from = true;
51 return *this;
54 int val = 0;
55 int copies = 0;
56 bool moved_from = false;
59 Int operator+(Int x, Int y) { x.val += y.val; return x; }
60 Int operator*(Int x, Int y) { x.val *= y.val; return x; }
62 struct Add
64 Int operator()(Int x, Int y) const { x.val += y.val; return x; }
67 struct Multiply
69 Int operator()(Int x, Int y) const { x.val *= y.val; return x; }
72 void
73 test01()
75 Int i[] = { 0, 1, 2, 3, 4 };
76 Int j[] = { 5, 6, 7, 8, 9 };
77 Int res = std::inner_product(std::begin(i), std::end(i), std::begin(j),
78 Int{0});
79 VERIFY( res.copies == 0 );
80 VERIFY( !res.moved_from );
81 for (const auto& r : i)
82 VERIFY( !r.moved_from );
83 for (const auto& r : j)
84 VERIFY( !r.moved_from );
87 void
88 test02()
90 Int i[] = { 0, 1, 2, 3, 4 };
91 Int j[] = { 5, 6, 7, 8, 9 };
92 Int res = std::inner_product(std::begin(i), std::end(i), std::begin(j),
93 Int{0}, Add{}, Multiply{});
94 VERIFY( res.copies == 0 );
95 VERIFY( !res.moved_from );
96 for (const auto& r : i)
97 VERIFY( !r.moved_from );
98 for (const auto& r : j)
99 VERIFY( !r.moved_from );
103 main()
105 test01();
106 test02();