I see function objects used often together with STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Function objects were designed to allow a strong layer of abstraction over STL, and in that regard they are great.
However, I prefer to use
boost::bind
and bind a function to the STL algorithms instead -- usually (although not in cases where the object has a state) that seems a more elegant solution.Also, another upcoming alternative are lambda's in C++0x (example shamelessly stolen from Wikipedia):
Notice that due to closures, they do not have the restriction of bind about not having a state.
I can't say why they came about - possibly simply because they could!
When do you use a functor? Consider that a functor is just moving the code you'd normally put in a loop into the operator() of a class, they're not that much different from just calling a function in a while loop... except, by using them you allow the compiler to inline the code and you can also pass a pre-constructed object instead, that you've constructed with some state. That latter point makes them very powerful.
Compare the sort algorithm with the CRTs qsort call. They do the same thing, only do it quite differently.
Function objects (functors) are typically used instead of function pointers. Function pointers have the problem that the compiler typically passes them as raw pointers, which makes it hard for the compiler to inline the code later. And they're easier to give parameters.
As said jdv, functors are used instead of function pointers, that are harder to optimize and inline for the compiler; moreover, a fundamental advantage of functors is that they can easily preserve a state between calls to them1, so they can work differently depending on the other times they have been called, keep track somehow of the parameters they have used, ...
For example, if you want to sum all the elements in two containers of ints you may do something like this:
This article has a great in-depth look at function objects, and how it can make code much more powerful, and cleaner too.
A function object is a function that is also an object, i.e. it has state. Normal functions generally do not have state. They may emulate having state by accessing global variables, but then the state is shared across all invocations.