Hello,
I am reading Thinking in C++ V2, there is something I can't figure out.
The code in the book:
//: C06:CopyInts3.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Uses an output stream iterator.
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>
using namespace std;
bool gt15(int x) { return 15 < x; }
int main() {
int a[] = { 10, 20, 30 };
const size_t SIZE = sizeof a / sizeof a[0];
remove_copy_if(a, a + SIZE,
ostream_iterator<int>(cout, "\n"), gt15);
} ///:~
my question is from this line: remove_copy_if(a, a + SIZE,ostream_iterator<int>(cout, "\n"), gt15);
The gt15 is the name of the function so I think it is the address of function,
but the parameter here requires to be function object, why it is an address of funciton?
Here is the remove_copy_if code:
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator remove_copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred)
{
while (first!=last) {
if (!pred(*first)) {
*result = *first;
++result;
}
++first;
}
return result;
}
> but the parameter here requires to be function object
See how it is used pred(*first)
anything at which you can put parenthesis and pass an object of the correct type would be valid.