functional 17 KB

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