610 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP_FUNCTIONAL11#define _LIBCPP_FUNCTIONAL12 13/*14 functional synopsis15 16namespace std17{18 19template <class Arg, class Result>20struct unary_function21{22 typedef Arg argument_type;23 typedef Result result_type;24};25 26template <class Arg1, class Arg2, class Result>27struct binary_function28{29 typedef Arg1 first_argument_type;30 typedef Arg2 second_argument_type;31 typedef Result result_type;32};33 34template <class T>35class reference_wrapper36 : public unary_function<T1, R> // if wrapping a unary functor37 : public binary_function<T1, T2, R> // if wrapping a binary functor38{39public:40 // types41 typedef T type;42 typedef see below result_type; // Not always defined43 44 // construct/copy/destroy45 template<class U>46 constexpr reference_wrapper(U&&); // constexpr since C++2047 constexpr reference_wrapper(const reference_wrapper<T>& x) noexcept; // constexpr since C++2048 49 // assignment50 constexpr reference_wrapper&51 operator=(const reference_wrapper<T>& x) noexcept; // constexpr since C++2052 53 // access54 constexpr operator T& () const noexcept; // constexpr since C++2055 constexpr T& get() const noexcept; // constexpr since C++2056 57 // invoke58 template <class... ArgTypes>59 constexpr typename result_of<T&(ArgTypes&&...)>::type // constexpr since C++2060 operator() (ArgTypes&&...) const61 noexcept(is_nothrow_invocable_v<T&, ArgTypes...>); // noexcept since C++1762};63 64template <class T>65 reference_wrapper(T&) -> reference_wrapper<T>;66 67template <class T> reference_wrapper<T> ref(T& t) noexcept;68template <class T> void ref(const T&& t) = delete;69template <class T> reference_wrapper<T> ref(reference_wrapper<T>t) noexcept;70 71template <class T> reference_wrapper<const T> cref(const T& t) noexcept;72template <class T> void cref(const T&& t) = delete;73template <class T> reference_wrapper<const T> cref(reference_wrapper<T> t) noexcept;74 75template <class T> struct unwrap_reference; // since C++2076template <class T> struct unwrap_ref_decay : unwrap_reference<decay_t<T>> { }; // since C++2077template <class T> using unwrap_reference_t = typename unwrap_reference<T>::type; // since C++2078template <class T> using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type; // since C++2079 80// [refwrap.comparisons], comparisons81friend constexpr bool operator==(reference_wrapper, reference_wrapper); // Since C++2682friend constexpr bool operator==(reference_wrapper, const T&); // Since C++2683friend constexpr bool operator==(reference_wrapper, reference_wrapper<const T>); // Since C++2684 85friend constexpr auto operator<=>(reference_wrapper, reference_wrapper); // Since C++2686friend constexpr auto operator<=>(reference_wrapper, const T&); // Since C++2687friend constexpr auto operator<=>(reference_wrapper, reference_wrapper<const T>); // Since C++2688 89template <class T> // <class T=void> in C++1490struct plus {91 T operator()(const T& x, const T& y) const;92};93 94template <class T> // <class T=void> in C++1495struct minus {96 T operator()(const T& x, const T& y) const;97};98 99template <class T> // <class T=void> in C++14100struct multiplies {101 T operator()(const T& x, const T& y) const;102};103 104template <class T> // <class T=void> in C++14105struct divides {106 T operator()(const T& x, const T& y) const;107};108 109template <class T> // <class T=void> in C++14110struct modulus {111 T operator()(const T& x, const T& y) const;112};113 114template <class T> // <class T=void> in C++14115struct negate {116 T operator()(const T& x) const;117};118 119template <class T> // <class T=void> in C++14120struct equal_to {121 bool operator()(const T& x, const T& y) const;122};123 124template <class T> // <class T=void> in C++14125struct not_equal_to {126 bool operator()(const T& x, const T& y) const;127};128 129template <class T> // <class T=void> in C++14130struct greater {131 bool operator()(const T& x, const T& y) const;132};133 134template <class T> // <class T=void> in C++14135struct less {136 bool operator()(const T& x, const T& y) const;137};138 139template <class T> // <class T=void> in C++14140struct greater_equal {141 bool operator()(const T& x, const T& y) const;142};143 144template <class T> // <class T=void> in C++14145struct less_equal {146 bool operator()(const T& x, const T& y) const;147};148 149// [comparisons.three.way], class compare_three_way150struct compare_three_way;151 152template <class T> // <class T=void> in C++14153struct logical_and {154 bool operator()(const T& x, const T& y) const;155};156 157template <class T> // <class T=void> in C++14158struct logical_or {159 bool operator()(const T& x, const T& y) const;160};161 162template <class T> // <class T=void> in C++14163struct logical_not {164 bool operator()(const T& x) const;165};166 167template <class T> // <class T=void> in C++14168struct bit_and {169 T operator()(const T& x, const T& y) const;170};171 172template <class T> // <class T=void> in C++14173struct bit_or {174 T operator()(const T& x, const T& y) const;175};176 177template <class T> // <class T=void> in C++14178struct bit_xor {179 T operator()(const T& x, const T& y) const;180};181 182template <class T=void> // C++14183struct bit_not {184 T operator()(const T& x) const;185};186 187struct identity; // C++20188 189template <class Predicate>190class unary_negate // deprecated in C++17, removed in C++20191 : public unary_function<typename Predicate::argument_type, bool>192{193public:194 explicit unary_negate(const Predicate& pred);195 bool operator()(const typename Predicate::argument_type& x) const;196};197 198template <class Predicate> // deprecated in C++17, removed in C++20199unary_negate<Predicate> not1(const Predicate& pred);200 201template <class Predicate>202class binary_negate // deprecated in C++17, removed in C++20203 : public binary_function<typename Predicate::first_argument_type,204 typename Predicate::second_argument_type,205 bool>206{207public:208 explicit binary_negate(const Predicate& pred);209 bool operator()(const typename Predicate::first_argument_type& x,210 const typename Predicate::second_argument_type& y) const;211};212 213template <class Predicate> // deprecated in C++17, removed in C++20214binary_negate<Predicate> not2(const Predicate& pred);215 216template <class F>217 constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20218template <auto f>219 constexpr unspecified not_fn() noexcept; // C++26220 221// [func.bind.partial], function templates bind_front and bind_back222template<class F, class... Args>223 constexpr unspecified bind_front(F&&, Args&&...); // C++20224template<class F, class... Args>225 constexpr unspecified bind_back(F&&, Args&&...); // C++23226 227template<class T> struct is_bind_expression;228template<class T> struct is_placeholder;229 230 // See C++14 20.9.9, Function object binders231template <class T> inline constexpr bool is_bind_expression_v232 = is_bind_expression<T>::value; // C++17233template <class T> inline constexpr int is_placeholder_v234 = is_placeholder<T>::value; // C++17235 236 237template<class Fn, class... BoundArgs>238 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20239template<class R, class Fn, class... BoundArgs>240 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20241 242// [func.invoke]243template<class F, class... Args>244 constexpr // constexpr in C++20245 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17246 noexcept(is_nothrow_invocable_v<F, Args...>);247 248template<class R, class F, class... Args>249 constexpr R invoke_r(F&& f, Args&&... args) // C++23250 noexcept(is_nothrow_invocable_r_v<R, F, Args...>);251 252namespace placeholders {253 // M is the implementation-defined number of placeholders254 extern unspecified _1;255 extern unspecified _2;256 .257 .258 .259 extern unspecified _Mp;260}261 262template <class Operation>263class binder1st // deprecated in C++11, removed in C++17264 : public unary_function<typename Operation::second_argument_type,265 typename Operation::result_type>266{267protected:268 Operation op;269 typename Operation::first_argument_type value;270public:271 binder1st(const Operation& x, const typename Operation::first_argument_type y);272 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;273 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;274};275 276template <class Operation, class T>277binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17278 279template <class Operation>280class binder2nd // deprecated in C++11, removed in C++17281 : public unary_function<typename Operation::first_argument_type,282 typename Operation::result_type>283{284protected:285 Operation op;286 typename Operation::second_argument_type value;287public:288 binder2nd(const Operation& x, const typename Operation::second_argument_type y);289 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;290 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;291};292 293template <class Operation, class T>294binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17295 296template <class Arg, class Result> // deprecated in C++11, removed in C++17297class pointer_to_unary_function : public unary_function<Arg, Result>298{299public:300 explicit pointer_to_unary_function(Result (*f)(Arg));301 Result operator()(Arg x) const;302};303 304template <class Arg, class Result>305pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17306 307template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17308class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>309{310public:311 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));312 Result operator()(Arg1 x, Arg2 y) const;313};314 315template <class Arg1, class Arg2, class Result>316pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17317 318template<class S, class T> // deprecated in C++11, removed in C++17319class mem_fun_t : public unary_function<T*, S>320{321public:322 explicit mem_fun_t(S (T::*p)());323 S operator()(T* p) const;324};325 326template<class S, class T, class A>327class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17328{329public:330 explicit mem_fun1_t(S (T::*p)(A));331 S operator()(T* p, A x) const;332};333 334template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17335template<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++17336 337template<class S, class T>338class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17339{340public:341 explicit mem_fun_ref_t(S (T::*p)());342 S operator()(T& p) const;343};344 345template<class S, class T, class A>346class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17347{348public:349 explicit mem_fun1_ref_t(S (T::*p)(A));350 S operator()(T& p, A x) const;351};352 353template<class S, class T>354mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17355template<class S, class T, class A>356mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17357 358template <class S, class T>359class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17360{361public:362 explicit const_mem_fun_t(S (T::*p)() const);363 S operator()(const T* p) const;364};365 366template <class S, class T, class A>367class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17368{369public:370 explicit const_mem_fun1_t(S (T::*p)(A) const);371 S operator()(const T* p, A x) const;372};373 374template <class S, class T>375const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17376template <class S, class T, class A>377const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17378 379template <class S, class T>380class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17381{382public:383 explicit const_mem_fun_ref_t(S (T::*p)() const);384 S operator()(const T& p) const;385};386 387template <class S, class T, class A>388class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17389{390public:391 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);392 S operator()(const T& p, A x) const;393};394 395template <class S, class T>396const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17397template <class S, class T, class A>398const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17399 400template<class R, class T> constexpr unspecified mem_fn(R T::*) noexcept; // constexpr in C++20401 402class bad_function_call403 : public exception404{405};406 407template<class> class function; // undefined408 409template<class R, class... ArgTypes>410class function<R(ArgTypes...)>411 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and412 // ArgTypes contains T1413 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and414 // ArgTypes contains T1 and T2415{416public:417 typedef R result_type;418 419 // construct/copy/destroy:420 function() noexcept;421 function(nullptr_t) noexcept;422 function(const function&);423 function(function&&) noexcept;424 template<class F>425 function(F);426 template<Allocator Alloc>427 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17428 template<Allocator Alloc>429 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17430 template<Allocator Alloc>431 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17432 template<Allocator Alloc>433 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17434 template<class F, Allocator Alloc>435 function(allocator_arg_t, const Alloc&, F); // removed in C++17436 437 function& operator=(const function&);438 function& operator=(function&&) noexcept;439 function& operator=(nullptr_t) noexcept;440 template<class F>441 function& operator=(F&&);442 template<class F>443 function& operator=(reference_wrapper<F>) noexcept;444 445 ~function();446 447 // function modifiers:448 void swap(function&) noexcept;449 template<class F, class Alloc>450 void assign(F&&, const Alloc&); // Removed in C++17451 452 // function capacity:453 explicit operator bool() const noexcept;454 455 // function invocation:456 R operator()(ArgTypes...) const;457 458 // function target access:459 const std::type_info& target_type() const noexcept;460 template <typename T> T* target() noexcept;461 template <typename T> const T* target() const noexcept;462};463 464// Deduction guides465template<class R, class ...Args>466function(R(*)(Args...)) -> function<R(Args...)>; // since C++17467 468template<class F>469function(F) -> function<see-below>; // since C++17470 471// Null pointer comparisons:472template <class R, class ... ArgTypes>473 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;474 475template <class R, class ... ArgTypes>476 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20477 478template <class R, class ... ArgTypes>479 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept; // removed in C++20480 481template <class R, class ... ArgTypes>482 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20483 484// specialized algorithms:485template <class R, class ... ArgTypes>486 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;487 488template <class T> struct hash;489 490template <> struct hash<bool>;491template <> struct hash<char>;492template <> struct hash<signed char>;493template <> struct hash<unsigned char>;494template <> struct hash<char8_t>; // since C++20495template <> struct hash<char16_t>;496template <> struct hash<char32_t>;497template <> struct hash<wchar_t>;498template <> struct hash<short>;499template <> struct hash<unsigned short>;500template <> struct hash<int>;501template <> struct hash<unsigned int>;502template <> struct hash<long>;503template <> struct hash<long long>;504template <> struct hash<unsigned long>;505template <> struct hash<unsigned long long>;506 507template <> struct hash<float>;508template <> struct hash<double>;509template <> struct hash<long double>;510 511template<class T> struct hash<T*>;512template <> struct hash<nullptr_t>; // C++17513 514namespace ranges {515 // [range.cmp], concept-constrained comparisons516 struct equal_to;517 struct not_equal_to;518 struct greater;519 struct less;520 struct greater_equal;521 struct less_equal;522}523 524} // std525 526POLICY: For non-variadic implementations, the number of arguments is limited527 to 3. It is hoped that the need for non-variadic implementations528 will be minimal.529 530*/531 532#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)533# include <__cxx03/functional>534#else535# include <__config>536 537# include <__functional/binary_function.h>538# include <__functional/binary_negate.h>539# include <__functional/bind.h>540# include <__functional/binder1st.h>541# include <__functional/binder2nd.h>542# include <__functional/hash.h>543# include <__functional/mem_fn.h> // TODO: deprecate544# include <__functional/mem_fun_ref.h>545# include <__functional/operations.h>546# include <__functional/pointer_to_binary_function.h>547# include <__functional/pointer_to_unary_function.h>548# include <__functional/reference_wrapper.h>549# include <__functional/unary_function.h>550# include <__functional/unary_negate.h>551 552# ifndef _LIBCPP_CXX03_LANG553# include <__functional/function.h>554# endif555 556# if _LIBCPP_STD_VER >= 17557# include <__functional/boyer_moore_searcher.h>558# include <__functional/default_searcher.h>559# include <__functional/invoke.h>560# include <__functional/not_fn.h>561# endif562 563# if _LIBCPP_STD_VER >= 20564# include <__functional/bind_back.h>565# include <__functional/bind_front.h>566# include <__functional/identity.h>567# include <__functional/ranges_operations.h>568# include <__type_traits/common_reference.h>569# include <__type_traits/unwrap_ref.h>570# endif571 572# include <version>573 574# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)575# pragma GCC system_header576# endif577 578# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && defined(_LIBCPP_CXX03_LANG)579# include <limits>580# include <new>581# endif582 583# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 14584# include <array>585# include <initializer_list>586# include <unordered_map>587# endif588 589# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20590# include <atomic>591# include <concepts>592# include <cstdlib>593# include <exception>594# include <iosfwd>595# include <memory>596# include <stdexcept>597# include <tuple>598# include <type_traits>599# include <typeinfo>600# include <utility>601# include <vector>602# endif603 604# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER == 23605# include <__vector/vector.h>606# endif607#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)608 609#endif // _LIBCPP_FUNCTIONAL610