469,953 Members | 2,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

functors

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.
Jul 22 '05 #1
2 1340
"nsgi_2004" <no****@nospam.com> wrote in message
news:RMASc.27737$ih.15231@fed1read07...
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?

--
David Hilsee
Jul 22 '05 #2
nsgi_2004 wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by red floyd | last post: by
41 posts views Thread by AngleWyrm | last post: by
1 post views Thread by Matthias | last post: by
4 posts views Thread by Fraser Ross | last post: by
8 posts views Thread by michael.lesniak | last post: by
4 posts views Thread by tryptik | last post: by
2 posts views Thread by Jon Slaughter | last post: by
4 posts views Thread by Christopher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.