functors

N

nsgi_2004

Hi,

I have been learning about functors and at first everything was clear. Make
a class and overload operator () so that an object of the functor can be
thought of as a function.

However, I have seen in Effective STL, Scott passes normal functions into
places that expect functors. I'm not sure how this works, as I thought
functors had to be classes.

The only thing I can think of is that, because the STL algorithms are
templated, the template algorithm function will create a function with a
parameter that takes a pointer to the function being passed in as a functor.
I suppose now that I think about it, the compiler would know the function
type and could do this. If so this is very cool, because for a long time
I've needed this sort of functionality and had to resort to weird hacks.

Thanks in advance.
 
D

David Hilsee

nsgi_2004 said:
Hi,

I have been learning about functors and at first everything was clear. Make
a class and overload operator () so that an object of the functor can be
thought of as a function.

However, I have seen in Effective STL, Scott passes normal functions into
places that expect functors. I'm not sure how this works, as I thought
functors had to be classes.

The only thing I can think of is that, because the STL algorithms are
templated, the template algorithm function will create a function with a
parameter that takes a pointer to the function being passed in as a functor.
I suppose now that I think about it, the compiler would know the function
type and could do this. If so this is very cool, because for a long time
I've needed this sort of functionality and had to resort to weird hacks.

Thanks in advance.

Thanks for what? It sounds like you've got a solid grip on how one can
either pass an instance of a class or a pointer to a function to a template
function.

void function();

struct Functor {
void operator()();
};

template <class Func>
void foo( Func f ) {
f();
}

int main() {
// instantiates the template with an argument of void (*)()
// (a pointer to a function)
foo( &function );

// instantiates the template with an argument of Functor
foo( Functor() );
}

The latter can result in more efficient code because the compiler knows
exactly what function will be called for that instantiation and can
therefore inline it or otherwise optimize for it in the generated code.

Do you have any questions about this?
 
K

Karl Heinz Buchegger

nsgi_2004 said:
Hi,

I have been learning about functors and at first everything was clear. Make
a class and overload operator () so that an object of the functor can be
thought of as a function.

However, I have seen in Effective STL, Scott passes normal functions into
places that expect functors. I'm not sure how this works, as I thought
functors had to be classes.

Yep.

The thing is:

When the compiler tries to compile something like this:

foo();

Then there are 2 ways it can be done.
Either foo is a function,

void foo()
{
}

then the above compiles as ordinary function call.
Or foo is an object with an operator(). Then the above
compiles as calling the member operator() on object foo.

The piece that you see to be missing is that what you have
seen works with templates only. And the important part is,
that a template in itself is just a sort of blueprint. Only
after you specify the template parameters, the compiler takes
the template code, substitutes the template parameters everywhere
and compiles it. So when it comes to compilation, the compiler
already knows which one of the 2 versions from above applies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top