577 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___CXX03_FUNCTIONAL11#define _LIBCPP___CXX03_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>217constexpr unspecified not_fn(F&& f); // C++17, constexpr in C++20218 219// [func.bind.partial], function templates bind_front and bind_back220template<class F, class... Args>221 constexpr unspecified bind_front(F&&, Args&&...); // C++20222template<class F, class... Args>223 constexpr unspecified bind_back(F&&, Args&&...); // C++23224 225template<class T> struct is_bind_expression;226template<class T> struct is_placeholder;227 228 // See C++14 20.9.9, Function object binders229template <class T> inline constexpr bool is_bind_expression_v230 = is_bind_expression<T>::value; // C++17231template <class T> inline constexpr int is_placeholder_v232 = is_placeholder<T>::value; // C++17233 234 235template<class Fn, class... BoundArgs>236 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20237template<class R, class Fn, class... BoundArgs>238 constexpr unspecified bind(Fn&&, BoundArgs&&...); // constexpr in C++20239 240// [func.invoke]241template<class F, class... Args>242 constexpr // constexpr in C++20243 invoke_result_t<F, Args...> invoke(F&& f, Args&&... args) // C++17244 noexcept(is_nothrow_invocable_v<F, Args...>);245 246template<class R, class F, class... Args>247 constexpr R invoke_r(F&& f, Args&&... args) // C++23248 noexcept(is_nothrow_invocable_r_v<R, F, Args...>);249 250namespace placeholders {251 // M is the implementation-defined number of placeholders252 extern unspecified _1;253 extern unspecified _2;254 .255 .256 .257 extern unspecified _Mp;258}259 260template <class Operation>261class binder1st // deprecated in C++11, removed in C++17262 : public unary_function<typename Operation::second_argument_type,263 typename Operation::result_type>264{265protected:266 Operation op;267 typename Operation::first_argument_type value;268public:269 binder1st(const Operation& x, const typename Operation::first_argument_type y);270 typename Operation::result_type operator()( typename Operation::second_argument_type& x) const;271 typename Operation::result_type operator()(const typename Operation::second_argument_type& x) const;272};273 274template <class Operation, class T>275binder1st<Operation> bind1st(const Operation& op, const T& x); // deprecated in C++11, removed in C++17276 277template <class Operation>278class binder2nd // deprecated in C++11, removed in C++17279 : public unary_function<typename Operation::first_argument_type,280 typename Operation::result_type>281{282protected:283 Operation op;284 typename Operation::second_argument_type value;285public:286 binder2nd(const Operation& x, const typename Operation::second_argument_type y);287 typename Operation::result_type operator()( typename Operation::first_argument_type& x) const;288 typename Operation::result_type operator()(const typename Operation::first_argument_type& x) const;289};290 291template <class Operation, class T>292binder2nd<Operation> bind2nd(const Operation& op, const T& x); // deprecated in C++11, removed in C++17293 294template <class Arg, class Result> // deprecated in C++11, removed in C++17295class pointer_to_unary_function : public unary_function<Arg, Result>296{297public:298 explicit pointer_to_unary_function(Result (*f)(Arg));299 Result operator()(Arg x) const;300};301 302template <class Arg, class Result>303pointer_to_unary_function<Arg,Result> ptr_fun(Result (*f)(Arg)); // deprecated in C++11, removed in C++17304 305template <class Arg1, class Arg2, class Result> // deprecated in C++11, removed in C++17306class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>307{308public:309 explicit pointer_to_binary_function(Result (*f)(Arg1, Arg2));310 Result operator()(Arg1 x, Arg2 y) const;311};312 313template <class Arg1, class Arg2, class Result>314pointer_to_binary_function<Arg1,Arg2,Result> ptr_fun(Result (*f)(Arg1,Arg2)); // deprecated in C++11, removed in C++17315 316template<class S, class T> // deprecated in C++11, removed in C++17317class mem_fun_t : public unary_function<T*, S>318{319public:320 explicit mem_fun_t(S (T::*p)());321 S operator()(T* p) const;322};323 324template<class S, class T, class A>325class mem_fun1_t : public binary_function<T*, A, S> // deprecated in C++11, removed in C++17326{327public:328 explicit mem_fun1_t(S (T::*p)(A));329 S operator()(T* p, A x) const;330};331 332template<class S, class T> mem_fun_t<S,T> mem_fun(S (T::*f)()); // deprecated in C++11, removed in C++17333template<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++17334 335template<class S, class T>336class mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17337{338public:339 explicit mem_fun_ref_t(S (T::*p)());340 S operator()(T& p) const;341};342 343template<class S, class T, class A>344class mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17345{346public:347 explicit mem_fun1_ref_t(S (T::*p)(A));348 S operator()(T& p, A x) const;349};350 351template<class S, class T>352mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()); // deprecated in C++11, removed in C++17353template<class S, class T, class A>354mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A)); // deprecated in C++11, removed in C++17355 356template <class S, class T>357class const_mem_fun_t : public unary_function<const T*, S> // deprecated in C++11, removed in C++17358{359public:360 explicit const_mem_fun_t(S (T::*p)() const);361 S operator()(const T* p) const;362};363 364template <class S, class T, class A>365class const_mem_fun1_t : public binary_function<const T*, A, S> // deprecated in C++11, removed in C++17366{367public:368 explicit const_mem_fun1_t(S (T::*p)(A) const);369 S operator()(const T* p, A x) const;370};371 372template <class S, class T>373const_mem_fun_t<S,T> mem_fun(S (T::*f)() const); // deprecated in C++11, removed in C++17374template <class S, class T, class A>375const_mem_fun1_t<S,T,A> mem_fun(S (T::*f)(A) const); // deprecated in C++11, removed in C++17376 377template <class S, class T>378class const_mem_fun_ref_t : public unary_function<T, S> // deprecated in C++11, removed in C++17379{380public:381 explicit const_mem_fun_ref_t(S (T::*p)() const);382 S operator()(const T& p) const;383};384 385template <class S, class T, class A>386class const_mem_fun1_ref_t : public binary_function<T, A, S> // deprecated in C++11, removed in C++17387{388public:389 explicit const_mem_fun1_ref_t(S (T::*p)(A) const);390 S operator()(const T& p, A x) const;391};392 393template <class S, class T>394const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const); // deprecated in C++11, removed in C++17395template <class S, class T, class A>396const_mem_fun1_ref_t<S,T,A> mem_fun_ref(S (T::*f)(A) const); // deprecated in C++11, removed in C++17397 398template<class R, class T> constexpr unspecified mem_fn(R T::*); // constexpr in C++20399 400class bad_function_call401 : public exception402{403};404 405template<class> class function; // undefined406 407template<class R, class... ArgTypes>408class function<R(ArgTypes...)>409 : public unary_function<T1, R> // iff sizeof...(ArgTypes) == 1 and410 // ArgTypes contains T1411 : public binary_function<T1, T2, R> // iff sizeof...(ArgTypes) == 2 and412 // ArgTypes contains T1 and T2413{414public:415 typedef R result_type;416 417 // construct/copy/destroy:418 function() noexcept;419 function(nullptr_t) noexcept;420 function(const function&);421 function(function&&) noexcept;422 template<class F>423 function(F);424 template<Allocator Alloc>425 function(allocator_arg_t, const Alloc&) noexcept; // removed in C++17426 template<Allocator Alloc>427 function(allocator_arg_t, const Alloc&, nullptr_t) noexcept; // removed in C++17428 template<Allocator Alloc>429 function(allocator_arg_t, const Alloc&, const function&); // removed in C++17430 template<Allocator Alloc>431 function(allocator_arg_t, const Alloc&, function&&); // removed in C++17432 template<class F, Allocator Alloc>433 function(allocator_arg_t, const Alloc&, F); // removed in C++17434 435 function& operator=(const function&);436 function& operator=(function&&) noexcept;437 function& operator=(nullptr_t) noexcept;438 template<class F>439 function& operator=(F&&);440 template<class F>441 function& operator=(reference_wrapper<F>) noexcept;442 443 ~function();444 445 // function modifiers:446 void swap(function&) noexcept;447 template<class F, class Alloc>448 void assign(F&&, const Alloc&); // Removed in C++17449 450 // function capacity:451 explicit operator bool() const noexcept;452 453 // function invocation:454 R operator()(ArgTypes...) const;455 456 // function target access:457 const std::type_info& target_type() const noexcept;458 template <typename T> T* target() noexcept;459 template <typename T> const T* target() const noexcept;460};461 462// Deduction guides463template<class R, class ...Args>464function(R(*)(Args...)) -> function<R(Args...)>; // since C++17465 466template<class F>467function(F) -> function<see-below>; // since C++17468 469// Null pointer comparisons:470template <class R, class ... ArgTypes>471 bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;472 473template <class R, class ... ArgTypes>474 bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20475 476template <class R, class ... ArgTypes>477 bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept; // removed in C++20478 479template <class R, class ... ArgTypes>480 bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept; // removed in C++20481 482// specialized algorithms:483template <class R, class ... ArgTypes>484 void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;485 486template <class T> struct hash;487 488template <> struct hash<bool>;489template <> struct hash<char>;490template <> struct hash<signed char>;491template <> struct hash<unsigned char>;492template <> struct hash<char8_t>; // since C++20493template <> struct hash<char16_t>;494template <> struct hash<char32_t>;495template <> struct hash<wchar_t>;496template <> struct hash<short>;497template <> struct hash<unsigned short>;498template <> struct hash<int>;499template <> struct hash<unsigned int>;500template <> struct hash<long>;501template <> struct hash<long long>;502template <> struct hash<unsigned long>;503template <> struct hash<unsigned long long>;504 505template <> struct hash<float>;506template <> struct hash<double>;507template <> struct hash<long double>;508 509template<class T> struct hash<T*>;510template <> struct hash<nullptr_t>; // C++17511 512namespace ranges {513 // [range.cmp], concept-constrained comparisons514 struct equal_to;515 struct not_equal_to;516 struct greater;517 struct less;518 struct greater_equal;519 struct less_equal;520}521 522} // std523 524POLICY: For non-variadic implementations, the number of arguments is limited525 to 3. It is hoped that the need for non-variadic implementations526 will be minimal.527 528*/529 530#include <__cxx03/__config>531 532#include <__cxx03/__functional/binary_function.h>533#include <__cxx03/__functional/binary_negate.h>534#include <__cxx03/__functional/bind.h>535#include <__cxx03/__functional/binder1st.h>536#include <__cxx03/__functional/binder2nd.h>537#include <__cxx03/__functional/hash.h>538#include <__cxx03/__functional/mem_fn.h> // TODO: deprecate539#include <__cxx03/__functional/mem_fun_ref.h>540#include <__cxx03/__functional/operations.h>541#include <__cxx03/__functional/pointer_to_binary_function.h>542#include <__cxx03/__functional/pointer_to_unary_function.h>543#include <__cxx03/__functional/reference_wrapper.h>544#include <__cxx03/__functional/unary_function.h>545#include <__cxx03/__functional/unary_negate.h>546 547#include <__cxx03/version>548 549#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)550# pragma GCC system_header551#endif552 553#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)554# include <__cxx03/limits>555# include <__cxx03/new>556#endif557 558#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)559# include <__cxx03/array>560# include <__cxx03/unordered_map>561# include <__cxx03/vector>562#endif563 564#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)565# include <__cxx03/atomic>566# include <__cxx03/cstdlib>567# include <__cxx03/exception>568# include <__cxx03/iosfwd>569# include <__cxx03/memory>570# include <__cxx03/stdexcept>571# include <__cxx03/type_traits>572# include <__cxx03/typeinfo>573# include <__cxx03/utility>574#endif575 576#endif // _LIBCPP___CXX03_FUNCTIONAL577