aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/20_util/is_nothrow_invocable/value.cc
blob: 7396df2ff52d945a1435f920d35b9aab0cdec788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2016-2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++17" }
// { dg-do compile }

#include <type_traits>

#ifndef IS_NT_INVOCABLE_DEFINED
template<typename... T>
  constexpr bool is_nt_invocable()
  {
    constexpr bool result = std::is_nothrow_invocable_v<T...>;
    static_assert(std::is_nothrow_invocable<T...>::value == result);
    return result;
  }

template<typename R, typename... T>
  constexpr bool is_nt_invocable_r()
  {
    constexpr bool result = std::is_nothrow_invocable_r_v<R, T...>;
    static_assert(std::is_nothrow_invocable_r<R, T...>::value == result);
    return result;
  }
#endif

void test01()
{
  struct T { T(int) { } };
  struct NT { NT(int) noexcept { } };
  struct Ex { explicit Ex(int) noexcept { } };

  using func_type = void(*)();
  static_assert( ! is_nt_invocable< func_type>(),     "");

#if __cpp_noexcept_function_type
  using func_type_nt = void(*)() noexcept;
  static_assert(   is_nt_invocable< func_type_nt >(),  "");
#endif

  struct X { };
  using mem_type = int X::*;

  static_assert( ! is_nt_invocable< mem_type >(),	"");
  static_assert( ! is_nt_invocable< mem_type, int >(),   "");
  static_assert( ! is_nt_invocable< mem_type, int& >(),	"");

  static_assert(   is_nt_invocable< mem_type, X& >(),          "");
  static_assert(   is_nt_invocable_r< int,   mem_type, X& >(), "");
  static_assert(   is_nt_invocable_r< int&,  mem_type, X& >(), "");
  static_assert(   is_nt_invocable_r< long,  mem_type, X& >(), "");
  static_assert( ! is_nt_invocable_r< long&, mem_type, X& >(),
		   "conversion fails, cannot bind long& to int");
  static_assert(   is_nt_invocable_r< int&,  mem_type, X* >(), "");

  static_assert( ! is_nt_invocable_r< T,  mem_type, X& >(),
		   "conversion throws");
  static_assert(   is_nt_invocable_r< NT, mem_type, X& >(), "");
  static_assert( ! is_nt_invocable_r< Ex, mem_type, X& >(),
		   "conversion fails, would use explicit constructor");

  using memfun_type = int (X::*)();

  static_assert( ! is_nt_invocable< memfun_type >(),       "no object");
  static_assert( ! is_nt_invocable< memfun_type, int >(),  "no object");
  static_assert( ! is_nt_invocable< memfun_type, int& >(), "no object");
  static_assert( ! is_nt_invocable< memfun_type, X& >(),   "call throws");
  static_assert( ! is_nt_invocable< memfun_type, X* >(),   "call throws");

  static_assert( ! is_nt_invocable_r< T,  memfun_type, X& >(), "call throws");
  static_assert( ! is_nt_invocable_r< NT, memfun_type, X& >(), "call throws");
  static_assert( ! is_nt_invocable_r< Ex, memfun_type, X& >(), "call throws");

#if __cpp_noexcept_function_type
  using memfun_type_nt = int (X::*)() noexcept;

  static_assert( ! is_nt_invocable< memfun_type_nt >(),	      "no object");
  static_assert( ! is_nt_invocable< memfun_type_nt, int >(),  "no object");
  static_assert( ! is_nt_invocable< memfun_type_nt, int& >(), "no object");
  static_assert(   is_nt_invocable< memfun_type_nt, X& >(),   "");
  static_assert(   is_nt_invocable< memfun_type_nt, X* >(),   "");

  static_assert( ! is_nt_invocable_r< T,  memfun_type_nt, X& >(),
		   "conversion throws");
  static_assert(   is_nt_invocable_r< NT, memfun_type_nt, X& >(), "");
  static_assert( ! is_nt_invocable_r< Ex, memfun_type_nt, X& >(),
		   "conversion fails, would use explicit constructor");
#endif

  struct F {
    int& operator()();
    long& operator()() const noexcept;
    short& operator()(int) &&;
    char& operator()(int) const& noexcept;
  private:
    void operator()(int, int) noexcept;
  };
  using CF = const F;

  static_assert( ! is_nt_invocable< F  >(), "call throws");
  static_assert(   is_nt_invocable< CF >(), "");

  static_assert( ! is_nt_invocable_r< int&,  F  >(), "call throws");
  static_assert(   is_nt_invocable_r< long&, CF >(), "");
  static_assert( ! is_nt_invocable_r< T,     F  >(), "call throws");
  static_assert( ! is_nt_invocable_r< NT,    F  >(), "call throws");
  static_assert( ! is_nt_invocable_r< Ex,    F  >(), "call throws");
  static_assert( ! is_nt_invocable_r< void,  F  >(), "call throws");
  static_assert( ! is_nt_invocable_r< T,     CF >(), "conversion throws");
  static_assert(   is_nt_invocable_r< NT,    CF >(), "" );
  static_assert( ! is_nt_invocable_r< Ex,    CF >(), "conversion fails");
  static_assert(   is_nt_invocable_r< void,  CF >(), "");

  static_assert( ! is_nt_invocable< F,   int >(), "call throws");
  static_assert(   is_nt_invocable< F&,  int >(), "");

  static_assert( ! is_nt_invocable_r< short&, F,   int >(),
		   "call throws" );
  static_assert(   is_nt_invocable_r< char&,  F&,  int >(), "");
  static_assert( ! is_nt_invocable_r< T,      F&,  int >(),
		   "conversion throws");
  static_assert(   is_nt_invocable_r< NT,     F&,  int >(), "");
  static_assert( ! is_nt_invocable_r< Ex,     F&,  int >(),
		   "conversion fails, would use explicit constructor");

  static_assert(   is_nt_invocable< CF,   int >(), "");
  static_assert(   is_nt_invocable< CF&,  int >(), "");

  static_assert(   is_nt_invocable_r< char&,  CF,  int >(), "");
  static_assert(   is_nt_invocable_r< char&,  CF&, int >(), "");
  static_assert(   is_nt_invocable_r< void,   CF&, int >(), "");

  static_assert( ! is_nt_invocable_r< T,      CF&, int >(),
		   "conversion throws");
  static_assert(   is_nt_invocable_r< NT,     CF&, int >(), "");
  static_assert( ! is_nt_invocable_r< Ex,     CF&, int >(),
		   "conversion fails, would use explicit constructor");
  static_assert(   is_nt_invocable_r< void,   CF&, int >(), "");

  static_assert( ! is_nt_invocable< F, int, int >(),
		   "would call private member");
  static_assert( ! is_nt_invocable_r<void, F, int, int >(),
		   "would call private member");
}