1 // { dg-do run { target c++17 }  }
2 
3 // Copyright (C) 2013-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 #include <testsuite_hooks.h>
22 
23 int counter = 0;
24 
25 struct mixin_counter
26 {
mixin_countermixin_counter27   mixin_counter() { ++counter; }
mixin_countermixin_counter28   mixin_counter(mixin_counter const&) { ++counter; }
~mixin_countermixin_counter29   ~mixin_counter() { --counter; }
30 };
31 
32 struct value_type : private mixin_counter
33 {
34   value_type() = default;
value_typevalue_type35   value_type(int) : state(1) { }
value_typevalue_type36   value_type(std::initializer_list<char>, const char*) : state(2) { }
37   int state = 0;
38 };
39 
main()40 int main()
41 {
42   using O = std::optional<value_type>;
43 
44   // Check emplace
45 
46   {
47     O o;
48     o.emplace();
49     VERIFY( o && o->state == 0 );
50   }
51   {
52     O o { std::in_place, 0 };
53     o.emplace();
54     VERIFY( o && o->state == 0 );
55   }
56 
57   {
58     O o;
59     o.emplace(0);
60     VERIFY( o && o->state == 1 );
61   }
62   {
63     O o { std::in_place };
64     o.emplace(0);
65     VERIFY( o && o->state == 1 );
66   }
67 
68   {
69     O o;
70     o.emplace({ 'a' }, "");
71     VERIFY( o && o->state == 2 );
72   }
73   {
74     O o { std::in_place };
75     o.emplace({ 'a' }, "");
76     VERIFY( o && o->state == 2 );
77   }
78   {
79     O o;
80     VERIFY(&o.emplace(0) == &*o);
81     VERIFY(&o.emplace({ 'a' }, "") == &*o);
82   }
83 
84   static_assert( !std::is_constructible<O, std::initializer_list<int>, int>(), "" );
85 
86   VERIFY( counter == 0 );
87 }
88