473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1537
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: red floyd | last post by:
Disclaimer: VS.NET 2003 (haven't checked any other compiler). I'm writing functors for my classes. Because the objects in my containers are large, I'm making my functors take const T& parameters....
41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
1
by: Matthias | last post by:
Hello, I basically want to implement a general resource manager which should have a caching and a loading policy so that I can exchange those. Here's some example code of how it should work: ...
4
by: Fraser Ross | last post by:
Functors taking 1 argument for operator() should inherit from unary_function and those with 2 arguments should inherit from binary_function. If a functor has zero arguments for its operator()...
8
by: michael.lesniak | last post by:
Hello, I'm learning C++ for a couple of days and play a bit with the algorithms provided in the STL. One thing I don't understand is the fact that classes inherited of functors have to be...
4
by: tryptik | last post by:
Hello all, I have a question about iterators. I have a container of functors that operate on an std::string. The functors go something like this: class Functor { std::string...
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
9
by: laikon | last post by:
dear, all: below is a function with a parameter of function pointer. void f(int a, int b, int (*fp)(int, int)) { std::cout << fp(a, b) << std::endl; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.