| 1 |
// { dg-do run { target c++20 } } |
| 2 |
|
| 3 |
// Copyright (C) 2020-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 <variant> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
|
| 23 |
struct aggressive_aggregate |
| 24 |
{ |
| 25 |
int a; |
| 26 |
int b; |
| 27 |
}; |
| 28 |
|
| 29 |
struct dumbo |
| 30 |
{ |
| 31 |
dumbo() = delete; |
| 32 |
}; |
| 33 |
|
| 34 |
void test_emplace() |
| 35 |
{ |
| 36 |
std::variant<aggressive_aggregate, dumbo> x; |
| 37 |
x.emplace<aggressive_aggregate>(1, 2); |
| 38 |
VERIFY(x.index() == 0); |
| 39 |
VERIFY(std::get<0>(x).a == 1); |
| 40 |
VERIFY(std::get<0>(x).b == 2); |
| 41 |
x.emplace<aggressive_aggregate>(1); |
| 42 |
VERIFY(x.index() == 0); |
| 43 |
VERIFY(std::get<0>(x).a == 1); |
| 44 |
VERIFY(std::get<0>(x).b == 0); |
| 45 |
x.emplace<aggressive_aggregate>(); |
| 46 |
VERIFY(x.index() == 0); |
| 47 |
VERIFY(std::get<0>(x).a == 0); |
| 48 |
VERIFY(std::get<0>(x).b == 0); |
| 49 |
} |
| 50 |
|
| 51 |
void test_in_place_type_construct() |
| 52 |
{ |
| 53 |
using Var = std::variant<aggressive_aggregate, dumbo>; |
| 54 |
Var x{std::in_place_type<aggressive_aggregate>, 1,2}; |
| 55 |
VERIFY(x.index() == 0); |
| 56 |
VERIFY(std::get<0>(x).a == 1); |
| 57 |
VERIFY(std::get<0>(x).b == 2); |
| 58 |
Var y{std::in_place_type<aggressive_aggregate>, 1}; |
| 59 |
VERIFY(y.index() == 0); |
| 60 |
VERIFY(std::get<0>(y).a == 1); |
| 61 |
VERIFY(std::get<0>(y).b == 0); |
| 62 |
Var z{std::in_place_type<aggressive_aggregate>}; |
| 63 |
VERIFY(z.index() == 0); |
| 64 |
VERIFY(std::get<0>(z).a == 0); |
| 65 |
VERIFY(std::get<0>(z).b == 0); |
| 66 |
} |
| 67 |
|
| 68 |
void test_in_place_index_construct() |
| 69 |
{ |
| 70 |
using Var = std::variant<aggressive_aggregate, dumbo>; |
| 71 |
Var x{std::in_place_index<0>, 1,2}; |
| 72 |
VERIFY(x.index() == 0); |
| 73 |
VERIFY(std::get<0>(x).a == 1); |
| 74 |
VERIFY(std::get<0>(x).b == 2); |
| 75 |
Var y{std::in_place_index<0>, 1}; |
| 76 |
VERIFY(y.index() == 0); |
| 77 |
VERIFY(std::get<0>(y).a == 1); |
| 78 |
VERIFY(std::get<0>(y).b == 0); |
| 79 |
Var z{std::in_place_index<0>}; |
| 80 |
VERIFY(z.index() == 0); |
| 81 |
VERIFY(std::get<0>(z).a == 0); |
| 82 |
VERIFY(std::get<0>(z).b == 0); |
| 83 |
} |
| 84 |
|
| 85 |
int main() |
| 86 |
{ |
| 87 |
test_emplace(); |
| 88 |
test_in_place_type_construct(); |
| 89 |
test_in_place_index_construct(); |
| 90 |
} |