1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_FUNCTIONAL 11#define _LIBCPP_FUNCTIONAL 12 13/* 14 functional synopsis 15 16namespace std 17{ 18 19template <class Arg, class Result> 20struct unary_function 21{ 22 typedef Arg argument_type; 23 typedef Result result_type; 24}; 25 26template <class Arg1, class Arg2, class Result> 27struct binary_function 28{ 29 typedef Arg1 first_argument_type; 30 typedef Arg2 second_argument_type; 31 typedef Result result_type; 32}; 33 34template <class T> 35class reference_wrapper 36 : public unary_function<T1, R> // if wrapping a unary functor 37 : public binary_function<T1, T2, R> // if wraping a binary functor 38{ 39public: 40 // types 41 typedef T type; 42 typedef see below result_type; // Not always defined 43 44 // construct/copy/destroy 45 template<class U> 46 reference_wrapper(U&&); 47 reference_wrapper(const reference_wrapper<T>& x) noexcept; 48 49 // assignment 50 reference_wrapper& operator=(const reference_wrapper<T>& x) noexcept; 51 52 // access 53 operator T& () const noexcept; 54 T& get() const noexcept; 55 56 // invoke 57 template <class... ArgTypes> 58 typename result_of<T&(ArgTypes&&...)>::type 59 operator() (ArgTypes&&...) const; 60}; 61 62template <class T> 63 reference_wrapper(T&) -> reference_wrapper<T>; 64 65template <class T> reference_wrapper<T> ref(T& t) noexcept; 66template <class T> void ref(const T&& t) = delete; 67template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept; 68 69template <class T> reference_wrapper<const T> cref(const T& t) noexcept; 70template <class T> void cref(const T&& t) = delete; 71template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept; 72 73template <class T> struct unwrap_reference; // since C++20 74template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++20 75template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++20 76template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++20 77 78template <class T> // <class T=void> in C++14 79struct plus { 80 T operator()(const T& x, const T& y) const; 81}; 82 83template <class T> // <class T=void> in C++14 84struct minus { 85 T operator()(const T& x, const T& y) const; 86}; 87 88template <class T> // <class T=void> in C++14 89struct multiplies { 90 T operator()(const T& x, const T& y) const; 91}; 92 93template <class T> // <class T=void> in C++14 94struct divides { 95 T operator()(const T& x, const T& y) const; 96}; 97 98template <class T> // <class T=void> in C++14 99struct modulus { 100 T operator()(const T& x, const T& y) const; 101}; 102 103template <class T> // <class T=void> in C++14 104struct negate { 105 T operator()(const T& x) const; 106}; 107 108template <class T> // <class T=void> in C++14 109struct equal_to { 110 bool operator()(const T& x, const T& y) const; 111}; 112 113template <class T> // <class T=void> in C++14 114struct not_equal_to { 115 bool operator()(const T& x, const T& y) const; 116}; 117 118template <class T> // <class T=void> in C++14 119struct greater { 120 bool operator()(const T& x, const T& y) const; 121}; 122 123template <class T> // <class T=void> in C++14 124struct less { 125 bool operator()(const T& x, const T& y) const; 126}; 127 128template <class T> // <class T=void> in C++14 129struct greater_equal { 130 bool operator()(const T& x, const T& y) const; 131}; 132 133template <class T> // <class T=void> in C++14 134struct less_equal { 135 bool operator()(const T& x, const T& y) const; 136}; 137 138template <class T> // <class T=void> in C++14 139struct logical_and { 140 bool operator()(const T& x, const T& y) const; 141}; 142 143template <class T> // <class T=void> in C++14 144struct logical_or { 145 bool operator()(const T& x, const T& y) const; 146}; 147 148template <class T> // <class T=void> in C++14 149struct logical_not { 150 bool operator()(const T& x) const; 151}; 152 153template <class T> // <class T=void> in C++14 154struct bit_and { 155 T operator()(const T& x, const T& y) const; 156}; 157 158template <class T> // <class T=void> in C++14 159struct bit_or { 160 T operator()(const T& x, const T& y) const; 161}; 162 163template <class T> // <class T=void> in C++14 164struct bit_xor { 165 T operator()(const T& x, const T& y) const; 166}; 167 168template <class T=void> // C++14 169struct bit_not { 170 T operator()(const T& x) const; 171}; 172 173struct identity; // C++20 174 175template <class Predicate> 176class unary_negate // deprecated in C++17, removed in C++20 177 : public unary_function<typename Predicate::argument_type, bool> 178{ 179public: 180 explicit unary_negate(const Predicate& pred); 181 bool operator()(const typename Predicate::argument_type& x) const; 182}; 183 184template <class Predicate> // deprecated in C++17, removed in C++20 185unary_negate<Predicate> not1(const Predicate& pred); 186 187template <class Predicate> 188class binary_negate // deprecated in C++17, removed in C++20 189 : public binary_function<typename Predicate::first_argument_type, 190 typename Predicate::second_argument_type, 191 bool> 192{ 193public: 194 explicit binary_negate(const Predicate& pred); 195 bool operator()(const typename Predicate::first_argument_type& x, 196 const typename Predicate::second_argument_type& y) const; 197}; 198 199template <class Predicate> // deprecated in C++17, removed in C++20 200binary_negate<Predicate> not2(const Predicate& pred); 201 202template <class F> 203constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20 204 205template<class T> struct is_bind_expression; 206template<class T> struct is_placeholder; 207 208 // See C++14 20.9.9, Function object binders 209template <class T> inline constexpr bool is_bind_expression_v 210 = is_bind_expression<T>::value; // C++17 211template <class T> inline constexpr int is_placeholder_v 212 = is_placeholder<T>::value; // C++17 213 214 215template<class Fn, class... BoundArgs> 216 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20 217template<class R, class Fn, class... BoundArgs> 218 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20 219 220template<class F, class... Args> 221 constexpr // constexpr in C++20 222 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17 223 noexcept(is_nothrow_invocable_v<F, Args...>); 224 225namespace placeholders { 226 // M is the implementation-defined number of placeholders 227 extern unspecified _1; 228 extern unspecified _2; 229 . 230 . 231 . 232 extern unspecified _Mp; 233} 234 235template <class Operation> 236class binder1st // deprecated in C++11, removed in C++17 237 : public unary_function<typename Operation::second_argument_type, 238 typename Operation::result_type> 239{ 240protected: 241 Operation op; 242 typename Operation::first_argument_type value; 243public: 244 binder1st(const Operation& x, const typename Operation::first_argument_type y); 245 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const; 246 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const; 247}; 248 249template <class Operation, class T> 250binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17 251 252template <class Operation> 253class binder2nd // deprecated in C++11, removed in C++17 254 : public unary_function<typename Operation::first_argument_type, 255 typename Operation::result_type> 256{ 257protected: 258 Operation op; 259 typename Operation::second_argument_type value; 260public: 261 binder2nd(const Operation& x, const typename Operation::second_argument_type y); 262 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const; 263 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const; 264}; 265 266template <class Operation, class T> 267binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17 268 269template <class Arg, class Result> // deprecated in C++11, removed in C++17 270class pointer_to_unary_function : public unary_function<Arg, Result> 271{ 272public: 273 explicit pointer_to_unary_function(Result (*f)(Arg)); 274 Result operator()(Arg x) const; 275}; 276 277template <class Arg, class Result> 278pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17 279 280template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17 281class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> 282{ 283public: 284 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2)); 285 Result operator()(Arg1 x, Arg2 y) const; 286}; 287 288template <class Arg1, class Arg2, class Result> 289pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17 290 291template<class S, class T> // deprecated in C++11, removed in C++17 292class mem_fun_t : public unary_function<T*, S> 293{ 294public: 295 explicit mem_fun_t(S (T::*p)()); 296 S operator()(T* p) const; 297}; 298 299template<class S, class T, class A> 300class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17 301{ 302public: 303 explicit mem_fun1_t(S (T::*p)(A)); 304 S operator()(T* p, A x) const; 305}; 306 307template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17 308template<class S, class T, class A> mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A)); // deprecated in C++11, removed in C++17 309 310template<class S, class T> 311class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17 312{ 313public: 314 explicit mem_fun_ref_t(S (T::*p)()); 315 S operator()(T& p) const; 316}; 317 318template<class S, class T, class A> 319class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17 320{ 321public: 322 explicit mem_fun1_ref_t(S (T::*p)(A)); 323 S operator()(T& p, A x) const; 324}; 325 326template<class S, class T> mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17 327template<class S, class T, class A> mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17 328 329template <class S, class T> 330class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17 331{ 332public: 333 explicit const_mem_fun_t(S (T::*p)() const); 334 S operator()(const T* p) const; 335}; 336 337template <class S, class T, class A> 338class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17 339{ 340public: 341 explicit const_mem_fun1_t(S (T::*p)(A) const); 342 S operator()(const T* p, A x) const; 343}; 344 345template <class S, class T> const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17 346template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17 347 348template <class S, class T> 349class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17 350{ 351public: 352 explicit const_mem_fun_ref_t(S (T::*p)() const); 353 S operator()(const T& p) const; 354}; 355 356template <class S, class T, class A> 357class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17 358{ 359public: 360 explicit const_mem_fun1_ref_t(S (T::*p)(A) const); 361 S operator()(const T& p, A x) const; 362}; 363 364template <class S, class T> const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17 365template <class S, class T, class A> const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17 366 367template<class R, class T> 368constexpr unspecified mem_fn(R T::*); // constexpr in C++20 369 370class bad_function_call 371 : public exception 372{ 373}; 374 375template<class> class function; // undefined 376 377template<class R, class... ArgTypes> 378class function<R(ArgTypes...)> 379 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and 380 // ArgTypes contains T1 381 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and 382 // ArgTypes contains T1 and T2 383{ 384public: 385 typedef R result_type; 386 387 // construct/copy/destroy: 388 function() noexcept; 389 function(nullptr_t) noexcept; 390 function(const function&); 391 function(function&&) noexcept; 392 template<class F> 393 function(F); 394 template<Allocator Alloc> 395 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17 396 template<Allocator Alloc> 397 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17 398 template<Allocator Alloc> 399 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17 400 template<Allocator Alloc> 401 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17 402 template<class F, Allocator Alloc> 403 function(allocator_arg_t, const Alloc&, F); // removed in C++17 404 405 function& operator=(const function&); 406 function& operator=(function&&) noexcept; 407 function& operator=(nullptr_t) noexcept; 408 template<class F> 409 function& operator=(F&&); 410 template<class F> 411 function& operator=(reference_wrapper<F>) noexcept; 412 413 ~function(); 414 415 // function modifiers: 416 void swap(function&) noexcept; 417 template<class F, class Alloc> 418 void assign(F&&, const Alloc&); // Removed in C++17 419 420 // function capacity: 421 explicit operator bool() const noexcept; 422 423 // function invocation: 424 R operator()(ArgTypes...) const; 425 426 // function target access: 427 const std::type_info& target_type() const noexcept; 428 template <typename T> T* target() noexcept; 429 template <typename T> const T* target() const noexcept; 430}; 431 432// Deduction guides 433template<class R, class ...Args> 434function(R(*)(Args...)) -> function<R(Args...)>; // since C++17 435 436template<class F> 437function(F) -> function<see-below>; // since C++17 438 439// Null pointer comparisons: 440template <class R, class ... ArgTypes> 441 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept; 442 443template <class R, class ... ArgTypes> 444 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept; 445 446template <class R, class ... ArgTypes> 447 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept; 448 449template <class R, class ... ArgTypes> 450 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept; 451 452// specialized algorithms: 453template <class R, class ... ArgTypes> 454 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept; 455 456template <class T> struct hash; 457 458template <> struct hash<bool>; 459template <> struct hash<char>; 460template <> struct hash<signed char>; 461template <> struct hash<unsigned char>; 462template <> struct hash<char8_t>; // since C++20 463template <> struct hash<char16_t>; 464template <> struct hash<char32_t>; 465template <> struct hash<wchar_t>; 466template <> struct hash<short>; 467template <> struct hash<unsigned short>; 468template <> struct hash<int>; 469template <> struct hash<unsigned int>; 470template <> struct hash<long>; 471template <> struct hash<long long>; 472template <> struct hash<unsigned long>; 473template <> struct hash<unsigned long long>; 474 475template <> struct hash<float>; 476template <> struct hash<double>; 477template <> struct hash<long double>; 478 479template<class T> struct hash<T*>; 480template <> struct hash<nullptr_t>; // C++17 481 482} // std 483 484POLICY: For non-variadic implementations, the number of arguments is limited 485 to 3. It is hoped that the need for non-variadic implementations 486 will be minimal. 487 488*/ 489 490#include <__algorithm/search.h> 491#include <__config> 492#include <__debug> 493#include <__functional/binary_function.h> // TODO: deprecate 494#include <__functional/binary_negate.h> 495#include <__functional/bind_front.h> 496#include <__functional/bind.h> 497#include <__functional/binder1st.h> 498#include <__functional/binder2nd.h> 499#include <__functional/default_searcher.h> 500#include <__functional/function.h> 501#include <__functional/hash.h> 502#include <__functional/identity.h> 503#include <__functional/invoke.h> 504#include <__functional/mem_fn.h> // TODO: deprecate 505#include <__functional/mem_fun_ref.h> 506#include <__functional/not_fn.h> 507#include <__functional/operations.h> 508#include <__functional/pointer_to_binary_function.h> 509#include <__functional/pointer_to_unary_function.h> 510#include <__functional/ranges_operations.h> 511#include <__functional/reference_wrapper.h> 512#include <__functional/unary_function.h> // TODO: deprecate 513#include <__functional/unary_negate.h> 514#include <__functional/unwrap_ref.h> 515#include <__utility/forward.h> 516#include <concepts> 517#include <exception> 518#include <memory> 519#include <tuple> 520#include <type_traits> 521#include <typeinfo> 522#include <utility> 523#include <version> 524 525#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 526#pragma GCC system_header 527#endif 528 529#endif // _LIBCPP_FUNCTIONAL 530