| 1 | // Copyright (C) 2017-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 compile { target c++17 } } |
| 19 | // { dg-require-effective-target hosted } |
| 20 | |
| 21 | #include <functional> |
| 22 | |
| 23 | template<typename T, typename U> struct require_same; |
| 24 | template<typename T> struct require_same<T, T> { using type = void; }; |
| 25 | |
| 26 | template<typename T, typename U> |
| 27 | typename require_same<T, U>::type |
| 28 | check_type(U&) { } |
| 29 | |
| 30 | void f0v(); |
| 31 | void f0vn() noexcept; |
| 32 | int f0i(); |
| 33 | int f0in() noexcept; |
| 34 | long f1l(int&); |
| 35 | long f1ln(double*) noexcept; |
| 36 | |
| 37 | void |
| 38 | test01() |
| 39 | { |
| 40 | std::function func1 = f0v; |
| 41 | check_type<std::function<void()>>(func1); |
| 42 | |
| 43 | std::function func2 = f0vn; |
| 44 | check_type<std::function<void()>>(func2); |
| 45 | |
| 46 | std::function func3 = f0i; |
| 47 | check_type<std::function<int()>>(func3); |
| 48 | |
| 49 | std::function func4 = f0in; |
| 50 | check_type<std::function<int()>>(func4); |
| 51 | |
| 52 | std::function func5 = f1l; |
| 53 | check_type<std::function<long(int&)>>(func5); |
| 54 | |
| 55 | std::function func6 = f1ln; |
| 56 | check_type<std::function<long(double*)>>(func6); |
| 57 | |
| 58 | std::function func5a = func5; |
| 59 | check_type<std::function<long(int&)>>(func5a); |
| 60 | |
| 61 | std::function func6a = func6; |
| 62 | check_type<std::function<long(double*)>>(func6a); |
| 63 | } |
| 64 | |
| 65 | struct X { |
| 66 | int operator()(const short&, void*); |
| 67 | }; |
| 68 | |
| 69 | struct Y { |
| 70 | void operator()(int) const & noexcept; |
| 71 | }; |
| 72 | |
| 73 | void |
| 74 | test02() |
| 75 | { |
| 76 | X x; |
| 77 | std::function func1 = x; |
| 78 | check_type<std::function<int(const short&, void*)>>(func1); |
| 79 | |
| 80 | Y y; |
| 81 | std::function func2 = y; |
| 82 | check_type<std::function<void(int)>>(func2); |
| 83 | |
| 84 | std::function func3 = [&x](float) -> X& { return x; }; |
| 85 | check_type<std::function<X&(float)>>(func3); |
| 86 | } |