| 1 |
// { dg-do run { target c++17 } } |
| 2 |
|
| 3 |
// Copyright (C) 2013-2025 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 |
#include <vector> |
| 23 |
#include <tuple> |
| 24 |
|
| 25 |
struct combined { |
| 26 |
std::vector<int> v; |
| 27 |
std::tuple<int, int> t; |
| 28 |
template<class... Args> |
| 29 |
combined(std::initializer_list<int> il, Args&&... args) |
| 30 |
: v(il), t(std::forward<Args>(args)...) |
| 31 |
{ |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
int main() |
| 36 |
{ |
| 37 |
const int i = 42; |
| 38 |
auto o = std::make_optional(i); |
| 39 |
static_assert( std::is_same<decltype(o), std::optional<int>>(), "" ); |
| 40 |
VERIFY( o && *o == 42 ); |
| 41 |
VERIFY( &*o != &i ); |
| 42 |
auto o2 = std::make_optional<std::tuple<int, int>>(1, 2); |
| 43 |
static_assert( std::is_same<decltype(o2), |
| 44 |
std::optional<std::tuple<int, int>>>(), "" ); |
| 45 |
VERIFY( o2 && std::get<0>(*o2) == 1 && std::get<1>(*o2) == 2); |
| 46 |
auto o3 = std::make_optional<std::vector<int>>({42, 666}); |
| 47 |
static_assert( std::is_same<decltype(o3), |
| 48 |
std::optional<std::vector<int>>>(), "" ); |
| 49 |
VERIFY(o3 && (*o3)[0] == 42 && (*o3)[1] == 666); |
| 50 |
auto o4 = std::make_optional<combined>({42, 666}); |
| 51 |
static_assert( std::is_same<decltype(o4), |
| 52 |
std::optional<combined>>(), "" ); |
| 53 |
VERIFY(o4 && (o4->v)[0] == 42 && (o4->v)[1] == 666 |
| 54 |
&& std::get<0>(o4->t) == 0 && std::get<1>(o4->t) == 0 ); |
| 55 |
auto o5 = std::make_optional<combined>({1, 2}, 3, 4); |
| 56 |
static_assert( std::is_same<decltype(o5), |
| 57 |
std::optional<combined>>(), "" ); |
| 58 |
VERIFY(o4 && (o5->v)[0] == 1 && (o5->v)[1] == 2 |
| 59 |
&& std::get<0>(o5->t) == 3 && std::get<1>(o5->t) == 4 ); |
| 60 |
} |