1 // { dg-do compile { target c++17 } }
2 
3 // Copyright (C) 2018-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3.  If not see
18 // <http://www.gnu.org/licenses/>.
19 
20 #include <optional>
21 
f()22 constexpr bool f()
23 {
24   std::optional<int> o1{42};
25   std::optional<int> o2;
26   o2 = o1;
27   return (o1 == o2);
28 }
29 
f2()30 constexpr bool f2()
31 {
32   std::optional<int> o1{42};
33   std::optional<int> o2;
34   std::optional<int> o3;
35   o2 = o1;
36   o3 = std::move(o2);
37   return (o1 == o3);
38 }
39 
g()40 void g()
41 {
42   constexpr bool b = f();
43   static_assert(b);
44   constexpr bool b2 = f2();
45   static_assert(b2);
46 }
47 
48 struct NonTrivialButConstexpr
49 {
50   int dummy;
51   NonTrivialButConstexpr() = default;
NonTrivialButConstexprNonTrivialButConstexpr52   constexpr NonTrivialButConstexpr(int val) : dummy(val) {}
53   NonTrivialButConstexpr(const NonTrivialButConstexpr&) = default;
54   NonTrivialButConstexpr(NonTrivialButConstexpr&&) = default;
55   constexpr NonTrivialButConstexpr&
operator =NonTrivialButConstexpr56   operator=(const NonTrivialButConstexpr& other)
57   {
58     dummy = other.dummy;
59     return *this;
60   }
61   constexpr NonTrivialButConstexpr&
operator =NonTrivialButConstexpr62   operator=(NonTrivialButConstexpr&& other)
63   {
64     dummy = other.dummy;
65     return *this;
66   }
67 };
68 
f3()69 constexpr bool f3()
70 {
71   std::optional<NonTrivialButConstexpr> d1, d2;
72   d1 = d2;
73   std::optional<NonTrivialButConstexpr> o1{42};
74   std::optional<NonTrivialButConstexpr> o2{22};
75   o2 = o1;
76   return ((*o1).dummy == (*o2).dummy && o1->dummy == o2->dummy);
77 }
78 
f4()79 constexpr bool f4()
80 {
81   std::optional<NonTrivialButConstexpr> d1, d2;
82   d1 = std::move(d2);
83   std::optional<NonTrivialButConstexpr> o1{42};
84   std::optional<NonTrivialButConstexpr> o2{22};
85   std::optional<NonTrivialButConstexpr> o3{33};
86   o2 = o1;
87   o3 = std::move(o2);
88   return ((*o1).dummy == (*o3).dummy && o1->dummy == o3->dummy);
89 }
90 
g2()91 void g2()
92 {
93   constexpr bool b = f3();
94   static_assert(b);
95   constexpr bool b2 = f4();
96   static_assert(b2);
97 }
98