Quote:
Originally Posted by MrPickle
Okay, so I've took a look at the STL functions but I can't see where the function pointer define's the arguments.
I'll take for_each as an example:
- template <class In, class Op> Op for_each(In first, In last, Op f)
-
{
-
while(first != last) f(*first++);
-
return f;
-
}
I see no argument declaration for the function pointer, nor do I see a pointer declaration? (I copied the code from "The C++ Programming Language" by Bjarne Stoustrup so I'm 99% sure it's correct.
You see nothing about function pointers there because foreach is a template function. You can instantiate it with any type for
f so long as calling
f(*first++) is a legal operation. This means
f must be a function taking one argument or an object of a class with an overloaded
operator()() method taking one argument. Moreover because
first is of type
In,
f must take an argument convertible to whatever type
In is when the template is instantiated. (Similarly,
first and
last must be of a primitive or user-defined type which supports the
*,
!=, and
++ operators).
You may want to look into templates along with the STL and its notion of functors and predicates; you're likely to make heavy use of them if you do much work in C++.