STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stl_function.h
Go to the documentation of this file.
1 // Functor implementations -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996-1998
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
56 #ifndef _STL_FUNCTION_H
57 #define _STL_FUNCTION_H 1
58 
59 namespace std _GLIBCXX_VISIBILITY(default)
60 {
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 
63  // 20.3.1 base classes
100  template<typename _Arg, typename _Result>
101  struct unary_function
102  {
104  typedef _Arg argument_type;
105 
107  typedef _Result result_type;
108  };
109 
113  template<typename _Arg1, typename _Arg2, typename _Result>
114  struct binary_function
115  {
117  typedef _Arg1 first_argument_type;
118 
120  typedef _Arg2 second_argument_type;
121 
123  typedef _Result result_type;
124  };
127  // 20.3.2 arithmetic
138  template<typename _Tp>
140  struct plus : public binary_function<_Tp, _Tp, _Tp>
141  {
142  _Tp
143  operator()(const _Tp& __x, const _Tp& __y) const
144  { return __x + __y; }
145  };
146 
148  template<typename _Tp>
149  struct minus : public binary_function<_Tp, _Tp, _Tp>
150  {
151  _Tp
152  operator()(const _Tp& __x, const _Tp& __y) const
153  { return __x - __y; }
154  };
155 
157  template<typename _Tp>
158  struct multiplies : public binary_function<_Tp, _Tp, _Tp>
159  {
160  _Tp
161  operator()(const _Tp& __x, const _Tp& __y) const
162  { return __x * __y; }
163  };
164 
166  template<typename _Tp>
167  struct divides : public binary_function<_Tp, _Tp, _Tp>
168  {
169  _Tp
170  operator()(const _Tp& __x, const _Tp& __y) const
171  { return __x / __y; }
172  };
173 
175  template<typename _Tp>
176  struct modulus : public binary_function<_Tp, _Tp, _Tp>
177  {
178  _Tp
179  operator()(const _Tp& __x, const _Tp& __y) const
180  { return __x % __y; }
181  };
182 
184  template<typename _Tp>
185  struct negate : public unary_function<_Tp, _Tp>
186  {
187  _Tp
188  operator()(const _Tp& __x) const
189  { return -__x; }
190  };
193  // 20.3.3 comparisons
202  template<typename _Tp>
204  struct equal_to : public binary_function<_Tp, _Tp, bool>
205  {
206  bool
207  operator()(const _Tp& __x, const _Tp& __y) const
208  { return __x == __y; }
209  };
210 
212  template<typename _Tp>
213  struct not_equal_to : public binary_function<_Tp, _Tp, bool>
214  {
215  bool
216  operator()(const _Tp& __x, const _Tp& __y) const
217  { return __x != __y; }
218  };
219 
221  template<typename _Tp>
222  struct greater : public binary_function<_Tp, _Tp, bool>
223  {
224  bool
225  operator()(const _Tp& __x, const _Tp& __y) const
226  { return __x > __y; }
227  };
228 
230  template<typename _Tp>
231  struct less : public binary_function<_Tp, _Tp, bool>
232  {
233  bool
234  operator()(const _Tp& __x, const _Tp& __y) const
235  { return __x < __y; }
236  };
237 
239  template<typename _Tp>
240  struct greater_equal : public binary_function<_Tp, _Tp, bool>
241  {
242  bool
243  operator()(const _Tp& __x, const _Tp& __y) const
244  { return __x >= __y; }
245  };
246 
248  template<typename _Tp>
249  struct less_equal : public binary_function<_Tp, _Tp, bool>
250  {
251  bool
252  operator()(const _Tp& __x, const _Tp& __y) const
253  { return __x <= __y; }
254  };
257  // 20.3.4 logical operations
266  template<typename _Tp>
268  struct logical_and : public binary_function<_Tp, _Tp, bool>
269  {
270  bool
271  operator()(const _Tp& __x, const _Tp& __y) const
272  { return __x && __y; }
273  };
274 
276  template<typename _Tp>
277  struct logical_or : public binary_function<_Tp, _Tp, bool>
278  {
279  bool
280  operator()(const _Tp& __x, const _Tp& __y) const
281  { return __x || __y; }
282  };
283 
285  template<typename _Tp>
286  struct logical_not : public unary_function<_Tp, bool>
287  {
288  bool
289  operator()(const _Tp& __x) const
290  { return !__x; }
291  };
294  // _GLIBCXX_RESOLVE_LIB_DEFECTS
295  // DR 660. Missing Bitwise Operations.
296  template<typename _Tp>
297  struct bit_and : public binary_function<_Tp, _Tp, _Tp>
298  {
299  _Tp
300  operator()(const _Tp& __x, const _Tp& __y) const
301  { return __x & __y; }
302  };
303 
304  template<typename _Tp>
305  struct bit_or : public binary_function<_Tp, _Tp, _Tp>
306  {
307  _Tp
308  operator()(const _Tp& __x, const _Tp& __y) const
309  { return __x | __y; }
310  };
311 
312  template<typename _Tp>
313  struct bit_xor : public binary_function<_Tp, _Tp, _Tp>
314  {
315  _Tp
316  operator()(const _Tp& __x, const _Tp& __y) const
317  { return __x ^ __y; }
318  };
319 
320  // 20.3.5 negators
349  template<typename _Predicate>
351  class unary_negate
352  : public unary_function<typename _Predicate::argument_type, bool>
353  {
354  protected:
355  _Predicate _M_pred;
356 
357  public:
358  explicit
359  unary_negate(const _Predicate& __x) : _M_pred(__x) { }
360 
361  bool
362  operator()(const typename _Predicate::argument_type& __x) const
363  { return !_M_pred(__x); }
364  };
365 
367  template<typename _Predicate>
368  inline unary_negate<_Predicate>
369  not1(const _Predicate& __pred)
370  { return unary_negate<_Predicate>(__pred); }
371 
373  template<typename _Predicate>
374  class binary_negate
375  : public binary_function<typename _Predicate::first_argument_type,
376  typename _Predicate::second_argument_type, bool>
377  {
378  protected:
379  _Predicate _M_pred;
380 
381  public:
382  explicit
383  binary_negate(const _Predicate& __x) : _M_pred(__x) { }
384 
385  bool
386  operator()(const typename _Predicate::first_argument_type& __x,
387  const typename _Predicate::second_argument_type& __y) const
388  { return !_M_pred(__x, __y); }
389  };
390 
392  template<typename _Predicate>
393  inline binary_negate<_Predicate>
394  not2(const _Predicate& __pred)
395  { return binary_negate<_Predicate>(__pred); }
398  // 20.3.7 adaptors pointers functions
420  template<typename _Arg, typename _Result>
422  class pointer_to_unary_function : public unary_function<_Arg, _Result>
423  {
424  protected:
425  _Result (*_M_ptr)(_Arg);
426 
427  public:
428  pointer_to_unary_function() { }
429 
430  explicit
431  pointer_to_unary_function(_Result (*__x)(_Arg))
432  : _M_ptr(__x) { }
433 
434  _Result
435  operator()(_Arg __x) const
436  { return _M_ptr(__x); }
437  };
438 
440  template<typename _Arg, typename _Result>
441  inline pointer_to_unary_function<_Arg, _Result>
442  ptr_fun(_Result (*__x)(_Arg))
443  { return pointer_to_unary_function<_Arg, _Result>(__x); }
444 
446  template<typename _Arg1, typename _Arg2, typename _Result>
447  class pointer_to_binary_function
448  : public binary_function<_Arg1, _Arg2, _Result>
449  {
450  protected:
451  _Result (*_M_ptr)(_Arg1, _Arg2);
452 
453  public:
454  pointer_to_binary_function() { }
455 
456  explicit
457  pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2))
458  : _M_ptr(__x) { }
459 
460  _Result
461  operator()(_Arg1 __x, _Arg2 __y) const
462  { return _M_ptr(__x, __y); }
463  };
464 
466  template<typename _Arg1, typename _Arg2, typename _Result>
467  inline pointer_to_binary_function<_Arg1, _Arg2, _Result>
468  ptr_fun(_Result (*__x)(_Arg1, _Arg2))
469  { return pointer_to_binary_function<_Arg1, _Arg2, _Result>(__x); }
472  template<typename _Tp>
473  struct _Identity
474  : public unary_function<_Tp,_Tp>
475  {
476  _Tp&
477  operator()(_Tp& __x) const
478  { return __x; }
479 
480  const _Tp&
481  operator()(const _Tp& __x) const
482  { return __x; }
483  };
484 
485  template<typename _Pair>
486  struct _Select1st
487  : public unary_function<_Pair, typename _Pair::first_type>
488  {
489  typename _Pair::first_type&
490  operator()(_Pair& __x) const
491  { return __x.first; }
492 
493  const typename _Pair::first_type&
494  operator()(const _Pair& __x) const
495  { return __x.first; }
496 
497 #if __cplusplus >= 201103L
498  template<typename _Pair2>
499  typename _Pair2::first_type&
500  operator()(_Pair2& __x) const
501  { return __x.first; }
502 
503  template<typename _Pair2>
504  const typename _Pair2::first_type&
505  operator()(const _Pair2& __x) const
506  { return __x.first; }
507 #endif
508  };
509 
510  template<typename _Pair>
511  struct _Select2nd
512  : public unary_function<_Pair, typename _Pair::second_type>
513  {
514  typename _Pair::second_type&
515  operator()(_Pair& __x) const
516  { return __x.second; }
517 
518  const typename _Pair::second_type&
519  operator()(const _Pair& __x) const
520  { return __x.second; }
521  };
522 
523  // 20.3.8 adaptors pointers members
539  template<typename _Ret, typename _Tp>
542  class mem_fun_t : public unary_function<_Tp*, _Ret>
543  {
544  public:
545  explicit
546  mem_fun_t(_Ret (_Tp::*__pf)())
547  : _M_f(__pf) { }
548 
549  _Ret
550  operator()(_Tp* __p) const
551  { return (__p->*_M_f)(); }
552 
553  private:
554  _Ret (_Tp::*_M_f)();
555  };
556 
559  template<typename _Ret, typename _Tp>
560  class const_mem_fun_t : public unary_function<const _Tp*, _Ret>
561  {
562  public:
563  explicit
564  const_mem_fun_t(_Ret (_Tp::*__pf)() const)
565  : _M_f(__pf) { }
566 
567  _Ret
568  operator()(const _Tp* __p) const
569  { return (__p->*_M_f)(); }
570 
571  private:
572  _Ret (_Tp::*_M_f)() const;
573  };
574 
577  template<typename _Ret, typename _Tp>
578  class mem_fun_ref_t : public unary_function<_Tp, _Ret>
579  {
580  public:
581  explicit
582  mem_fun_ref_t(_Ret (_Tp::*__pf)())
583  : _M_f(__pf) { }
584 
585  _Ret
586  operator()(_Tp& __r) const
587  { return (__r.*_M_f)(); }
588 
589  private:
590  _Ret (_Tp::*_M_f)();
591  };
592 
595  template<typename _Ret, typename _Tp>
596  class const_mem_fun_ref_t : public unary_function<_Tp, _Ret>
597  {
598  public:
599  explicit
600  const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const)
601  : _M_f(__pf) { }
602 
603  _Ret
604  operator()(const _Tp& __r) const
605  { return (__r.*_M_f)(); }
606 
607  private:
608  _Ret (_Tp::*_M_f)() const;
609  };
610 
613  template<typename _Ret, typename _Tp, typename _Arg>
614  class mem_fun1_t : public binary_function<_Tp*, _Arg, _Ret>
615  {
616  public:
617  explicit
618  mem_fun1_t(_Ret (_Tp::*__pf)(_Arg))
619  : _M_f(__pf) { }
620 
621  _Ret
622  operator()(_Tp* __p, _Arg __x) const
623  { return (__p->*_M_f)(__x); }
624 
625  private:
626  _Ret (_Tp::*_M_f)(_Arg);
627  };
628 
631  template<typename _Ret, typename _Tp, typename _Arg>
632  class const_mem_fun1_t : public binary_function<const _Tp*, _Arg, _Ret>
633  {
634  public:
635  explicit
636  const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const)
637  : _M_f(__pf) { }
638 
639  _Ret
640  operator()(const _Tp* __p, _Arg __x) const
641  { return (__p->*_M_f)(__x); }
642 
643  private:
644  _Ret (_Tp::*_M_f)(_Arg) const;
645  };
646 
649  template<typename _Ret, typename _Tp, typename _Arg>
650  class mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
651  {
652  public:
653  explicit
654  mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg))
655  : _M_f(__pf) { }
656 
657  _Ret
658  operator()(_Tp& __r, _Arg __x) const
659  { return (__r.*_M_f)(__x); }
660 
661  private:
662  _Ret (_Tp::*_M_f)(_Arg);
663  };
664 
667  template<typename _Ret, typename _Tp, typename _Arg>
668  class const_mem_fun1_ref_t : public binary_function<_Tp, _Arg, _Ret>
669  {
670  public:
671  explicit
672  const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const)
673  : _M_f(__pf) { }
674 
675  _Ret
676  operator()(const _Tp& __r, _Arg __x) const
677  { return (__r.*_M_f)(__x); }
678 
679  private:
680  _Ret (_Tp::*_M_f)(_Arg) const;
681  };
682 
683  // Mem_fun adaptor helper functions. There are only two:
684  // mem_fun and mem_fun_ref.
685  template<typename _Ret, typename _Tp>
686  inline mem_fun_t<_Ret, _Tp>
687  mem_fun(_Ret (_Tp::*__f)())
688  { return mem_fun_t<_Ret, _Tp>(__f); }
689 
690  template<typename _Ret, typename _Tp>
691  inline const_mem_fun_t<_Ret, _Tp>
692  mem_fun(_Ret (_Tp::*__f)() const)
693  { return const_mem_fun_t<_Ret, _Tp>(__f); }
694 
695  template<typename _Ret, typename _Tp>
696  inline mem_fun_ref_t<_Ret, _Tp>
697  mem_fun_ref(_Ret (_Tp::*__f)())
698  { return mem_fun_ref_t<_Ret, _Tp>(__f); }
699 
700  template<typename _Ret, typename _Tp>
701  inline const_mem_fun_ref_t<_Ret, _Tp>
702  mem_fun_ref(_Ret (_Tp::*__f)() const)
703  { return const_mem_fun_ref_t<_Ret, _Tp>(__f); }
704 
705  template<typename _Ret, typename _Tp, typename _Arg>
706  inline mem_fun1_t<_Ret, _Tp, _Arg>
707  mem_fun(_Ret (_Tp::*__f)(_Arg))
708  { return mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
709 
710  template<typename _Ret, typename _Tp, typename _Arg>
711  inline const_mem_fun1_t<_Ret, _Tp, _Arg>
712  mem_fun(_Ret (_Tp::*__f)(_Arg) const)
713  { return const_mem_fun1_t<_Ret, _Tp, _Arg>(__f); }
714 
715  template<typename _Ret, typename _Tp, typename _Arg>
716  inline mem_fun1_ref_t<_Ret, _Tp, _Arg>
717  mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
718  { return mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
719 
720  template<typename _Ret, typename _Tp, typename _Arg>
721  inline const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
722  mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
723  { return const_mem_fun1_ref_t<_Ret, _Tp, _Arg>(__f); }
724 
727 _GLIBCXX_END_NAMESPACE_VERSION
728 } // namespace
729 
730 #if (__cplusplus < 201103L) || _GLIBCXX_USE_DEPRECATED
731 # include <backward/binders.h>
732 #endif
733 
734 #endif /* _STL_FUNCTION_H */
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36