]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/optional/make_optional.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / optional / make_optional.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run { target c++17 } }
3
4 // Copyright (C) 2013-2020 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <optional>
22 #include <testsuite_hooks.h>
23 #include <vector>
24 #include <tuple>
25
26 struct combined {
27 std::vector<int> v;
28 std::tuple<int, int> t;
29 template<class... Args>
30 combined(std::initializer_list<int> il, Args&&... args)
31 : v(il), t(std::forward<Args>(args)...)
32 {
33 }
34 };
35
36 int main()
37 {
38 const int i = 42;
39 auto o = std::make_optional(i);
40 static_assert( std::is_same<decltype(o), std::optional<int>>(), "" );
41 VERIFY( o && *o == 42 );
42 VERIFY( &*o != &i );
43 auto o2 = std::make_optional<std::tuple<int, int>>(1, 2);
44 static_assert( std::is_same<decltype(o2),
45 std::optional<std::tuple<int, int>>>(), "" );
46 VERIFY( o2 && std::get<0>(*o2) == 1 && std::get<1>(*o2) == 2);
47 auto o3 = std::make_optional<std::vector<int>>({42, 666});
48 static_assert( std::is_same<decltype(o3),
49 std::optional<std::vector<int>>>(), "" );
50 VERIFY(o3 && (*o3)[0] == 42 && (*o3)[1] == 666);
51 auto o4 = std::make_optional<combined>({42, 666});
52 static_assert( std::is_same<decltype(o4),
53 std::optional<combined>>(), "" );
54 VERIFY(o4 && (o4->v)[0] == 42 && (o4->v)[1] == 666
55 && std::get<0>(o4->t) == 0 && std::get<1>(o4->t) == 0 );
56 auto o5 = std::make_optional<combined>({1, 2}, 3, 4);
57 static_assert( std::is_same<decltype(o5),
58 std::optional<combined>>(), "" );
59 VERIFY(o4 && (o5->v)[0] == 1 && (o5->v)[1] == 2
60 && std::get<0>(o5->t) == 3 && std::get<1>(o5->t) == 4 );
61 }