| 1 | // Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++17 } } |
| 19 | // { dg-add-options no_pch } |
| 20 | |
| 21 | #include <tuple> |
| 22 | |
| 23 | #if __cpp_lib_apply < 201603 |
| 24 | # error "__cpp_lib_apply < 201603" |
| 25 | #endif |
| 26 | |
| 27 | #include <testsuite_hooks.h> |
| 28 | |
| 29 | void |
| 30 | test01() |
| 31 | { |
| 32 | auto t = std::make_tuple(1, '2', 3.0); |
| 33 | std::apply( [&](int& i, char& c, double& d) { |
| 34 | VERIFY(&i == &std::get<int>(t)); |
| 35 | VERIFY(&c == &std::get<char>(t)); |
| 36 | VERIFY(&d == &std::get<double>(t)); |
| 37 | }, t); |
| 38 | } |
| 39 | |
| 40 | constexpr int func(int i, int j) { return i + j; } |
| 41 | |
| 42 | void |
| 43 | test02() |
| 44 | { |
| 45 | constexpr auto t = std::make_tuple(1, 2); |
| 46 | constexpr int i = std::apply(func, t); |
| 47 | VERIFY( i == 3 ); |
| 48 | } |
| 49 | |
| 50 | struct F |
| 51 | { |
| 52 | int f(int i, int j) const { return i + j; } |
| 53 | }; |
| 54 | |
| 55 | void |
| 56 | test03() |
| 57 | { |
| 58 | auto t = std::make_tuple(F{}, 1, 2); |
| 59 | int r = std::apply(&F::f, t); |
| 60 | VERIFY( r == 3 ); |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | main() |
| 65 | { |
| 66 | test01(); |
| 67 | test02(); |
| 68 | test03(); |
| 69 | } |