I just saw this piece of code when I was looking at the stl source code analysis copy function
template<class InputIterator,class OutputIterator>
inline OutputIterator copy(InputIterator first,InputIterator last,OutputIterator result)
{
return __copy_dispatch<InputIterator,OutputIterator>()(fist,last,result);//这是个函数
}
//这是完全泛化的的版本。
template <class InputIterator,class OutputIterator>
struct __copy_dispatch
{
OutputIterator operator()(InputIterator first,InputIterator last,OutputIterator result)
{
return __copy(first,last,result,iterator_category(first));
}
};
This __copy_dispatch is a struct with an overloaded () operator. When called in copy, it directly
__copy_dispatch<InputIterator,OutputIterator>()(fist,last,result);
Directly The () operator is called using this struct, but is not called with a stuct object .
Is this okay? I quickly looked through c++primer and couldn't find the answer.
Please help me answer this question. thanks, thanks.
This is calling class
The default constructor of, its function is to generate a temporary object. Next
The function ofis to call operator() on this temporary object with first, last, and result as actual parameters.
Using this struct, the () operator is called. This is the meaning of functor. Its function here is to create an unnamed object
In fact,
lambda
also created a class that overloads operator() to achieve the effect of functor.