|
1 | 1 | --- |
2 | | -description: "Learn more about: Function Objects in the C++ Standard Library" |
3 | 2 | title: "Function Objects in the C++ Standard Library" |
4 | | -ms.date: "03/15/2019" |
| 3 | +description: "Learn more about: Function Objects in the C++ Standard Library" |
| 4 | +ms.date: 06/22/2025 |
5 | 5 | helpviewer_keywords: ["functors", "C++ Standard Library, functors", "C++ Standard Library, function objects", "function objects"] |
6 | | -ms.assetid: 85f8a735-2c7b-4f10-9c4d-95c666ec4192 |
7 | 6 | --- |
8 | 7 | # Function Objects in the C++ Standard Library |
9 | 8 |
|
10 | | -A *function object*, or *functor*, is any type that implements operator(). This operator is referred to as the *call operator* or sometimes the *application operator*. The C++ Standard Library uses function objects primarily as sorting criteria for containers and in algorithms. |
| 9 | +A *function object*, or *functor*, is any type that implements `operator()`. This operator is referred to as the *call operator* or sometimes the *application operator*. The C++ Standard Library uses function objects primarily as sorting criteria for containers and in algorithms. |
11 | 10 |
|
12 | | -Function objects provide two main advantages over a straight function call. The first is that a function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter. |
| 11 | +Function objects provide two main advantages over a regular function call. The first is that a function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter. |
13 | 12 |
|
14 | 13 | ## Creating a Function Object |
15 | 14 |
|
16 | | -To create a function object, create a type and implement operator(), such as: |
| 15 | +To create a function object, create a type and implement `operator()`, such as: |
17 | 16 |
|
18 | 17 | ```cpp |
19 | | -class Functor |
| 18 | +class LessThanFunctor |
20 | 19 | { |
21 | 20 | public: |
22 | | - int operator()(int a, int b) |
| 21 | + bool operator()(int a, int b) |
23 | 22 | { |
24 | 23 | return a < b; |
25 | 24 | } |
26 | 25 | }; |
27 | 26 |
|
28 | 27 | int main() |
29 | 28 | { |
30 | | - Functor f; |
| 29 | + LessThanFunctor less_than; |
31 | 30 | int a = 5; |
32 | 31 | int b = 7; |
33 | | - int ans = f(a, b); |
| 32 | + bool ans = less_than(a, b); |
34 | 33 | } |
35 | 34 | ``` |
36 | 35 |
|
37 | | -The last line of the `main` function shows how you call the function object. This call looks like a call to a function, but it's actually calling operator() of the Functor type. This similarity between calling a function object and a function is how the term function object came about. |
| 36 | +The last line of the `main` function shows how you call the function object. This call looks like a call to a function, but it's actually calling `operator()` of the `LessThanFunctor` type. This similarity between calling a function object and a function is how the term function object came about. |
38 | 37 |
|
39 | 38 | ## Function Objects and Containers |
40 | 39 |
|
41 | | -The C++ Standard Library contains several function objects in the [`<functional>`](../standard-library/functional.md) header file. One use of these function objects is as a sorting criterion for containers. For example, the `set` container is declared as follows: |
| 40 | +The C++ Standard Library contains several function objects in the [`<functional>`](functional.md) header file. One use of these function objects is as a sorting criterion for containers. For example, the [`set`](set-class.md) container is declared as follows: |
42 | 41 |
|
43 | 42 | ```cpp |
44 | 43 | template <class Key, |
45 | | - class Traits=less<Key>, |
46 | | - class Allocator=allocator<Key>> |
47 | | -class set |
| 44 | + class Compare = std::less<Key>, |
| 45 | + class Allocator = std::allocator<Key>> |
| 46 | +class set; |
48 | 47 | ``` |
49 | 48 |
|
50 | | -The second template argument is the function object `less`. This function object returns **`true`** if the first parameter is less than the second parameter. Since some containers sort their elements, the container needs a way of comparing two elements. The comparison is done by using the function object. You can define your own sorting criteria for containers by creating a function object and specifying it in the template list for the container. |
| 49 | +The second template argument is the function object [`less`](less-struct.md). This function object returns **`true`** if the first parameter is less than the second parameter. Since some containers sort their elements, the container needs a way of comparing two elements. The comparison is done by using the function object. You can define your own sorting criteria for containers by creating a function object and specifying it in the template list for the container. |
51 | 50 |
|
52 | 51 | ## Function Objects and Algorithms |
53 | 52 |
|
54 | | -Another use of functional objects is in algorithms. For example, the `remove_if` algorithm is declared as follows: |
| 53 | +Another use of function objects is in algorithms. For example, the [`remove_if`](algorithm-functions.md#remove_if) algorithm is declared as follows: |
55 | 54 |
|
56 | 55 | ```cpp |
57 | | -template <class ForwardIterator, class Predicate> |
| 56 | +template <class ForwardIterator, class UnaryPredicate> |
58 | 57 | ForwardIterator remove_if( |
59 | 58 | ForwardIterator first, |
60 | 59 | ForwardIterator last, |
61 | | - Predicate pred); |
| 60 | + UnaryPredicate pred); |
62 | 61 | ``` |
63 | 62 |
|
64 | | -The last argument to `remove_if` is a function object that returns a boolean value (a *predicate*). If the result of the function object is **`true`**, then the element is removed from the container being accessed by the iterators `first` and `last`. You can use any of the function objects declared in the [`<functional>`](../standard-library/functional.md) header for the argument `pred` or you can create your own. |
| 63 | +The last argument to `remove_if` is a function object that returns a boolean value (a *predicate*). If the result of the function object is **`true`**, then the element is shifted such that it's beyond the new end returned by `remove_if`. You can use any of the function objects declared in the [`<functional>`](functional.md) header for the argument `pred` or you can create your own. |
65 | 64 |
|
66 | 65 | ## See also |
67 | 66 |
|
68 | | -[C++ Standard Library Reference](../standard-library/cpp-standard-library-reference.md) |
| 67 | +[C++ Standard Library Reference](cpp-standard-library-reference.md) |
0 commit comments