Debian Codegraph @

libstdc++-v3/testsuite/20_util/function_objects/invoke/3.cc

raw
1 // Copyright (C) 2016-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-add-options no_pch }
20
21 #include <functional>
22
23 #ifndef __cpp_lib_invoke
24 # error Feature-test macro for invoke is missing in <functional>
25 #elif __cpp_lib_invoke < 201411L
26 # error Feature-test macro for invoke has the wrong value in <functional>
27 #endif
28
29 struct abstract {
30 virtual ~abstract() = 0;
31 void operator()() noexcept;
32 };
33
34 static_assert( noexcept(std::__invoke(std::declval<abstract>())),
35 "It should be possible to use abstract types with INVOKE" );
36 static_assert( noexcept(std::invoke(std::declval<abstract>())),
37 "It should be possible to use abstract types with INVOKE" );
38
39 // The std::__invoke_r extension only has a noexcept-specifier for >= C++17.
40 static_assert( noexcept(std::__invoke_r<void>(std::declval<abstract>())),
41 "It should be possible to use abstract types with INVOKE<R>" );
42
43 struct F {
44 void operator()() &;
45 void operator()() && noexcept;
46 int operator()(int);
47 double* operator()(int, int) noexcept;
48 };
49 struct D { D(void*); };
50
51 static_assert( !noexcept(std::__invoke(std::declval<F&>())), "" );
52 static_assert( noexcept(std::__invoke(std::declval<F>())), "" );
53 static_assert( !noexcept(std::__invoke(std::declval<F>(), 1)), "" );
54 static_assert( noexcept(std::__invoke(std::declval<F>(), 1, 2)), "" );
55
56 static_assert( !noexcept(std::invoke(std::declval<F&>())), "" );
57 static_assert( noexcept(std::invoke(std::declval<F>())), "" );
58 static_assert( !noexcept(std::invoke(std::declval<F>(), 1)), "" );
59 static_assert( noexcept(std::invoke(std::declval<F>(), 1, 2)), "" );
60
61 static_assert( !noexcept(std::__invoke_r<void>(std::declval<F&>())), "" );
62 static_assert( noexcept(std::__invoke_r<void>(std::declval<F>())), "" );
63 static_assert( !noexcept(std::__invoke_r<int>(std::declval<F>(), 1)), "" );
64 static_assert( !noexcept(std::__invoke_r<void>(std::declval<F>(), 1)), "" );
65 static_assert( !noexcept(std::__invoke_r<long>(std::declval<F>(), 1)), "" );
66 static_assert( noexcept(std::__invoke_r<void>(std::declval<F>(), 1, 2)), "" );
67 static_assert( noexcept(std::__invoke_r<void*>(std::declval<F>(), 1, 2)), "" );
68 static_assert( !noexcept(std::__invoke_r<D>(std::declval<F>(), 1, 2)), "" );