Template functions vs. function objects

R

richardclay09

Hi
Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as the FO
square_o in the following. When MUST you use one over the other, and
when MIGHT you use one over the other?
Thanks a bunch.....

#include <iostream.h>

template<class T> class Square {
public:
T operator()(const T& t) {
return t*t;
}
};

template<class T> T square_f(const T& t) {
return t*t;
}

int main(int argc, char **argv) {

Square<int> square_o;
cout << square_o(5);
cout << square_f(5);

return 0;
}
 
K

Kurt Krueckeberg

Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as the FO
square_o in the following. When MUST you use one over the other, and
when MIGHT you use one over the other?
Thanks a bunch.....

Function objects can be created, passed as parameters, or have their state
modified

Since a function object is an object, it can have unique state. Function
objects
can maintain that state between calls. This is nice in multi-thread
environments.

Function object are used in the standard library, where the member function,
say, operator ()(T&), can be inlined, avoiding the overhead of a function
call.
 
V

Victor Bazarov

Can someone please write some "compare and contrast" notes for
"Template functions vs. function objects"? When to use one and not the
other? For example, the TF square_f does the same thing as the FO
square_o in the following. When MUST you use one over the other, and
when MIGHT you use one over the other?

Is this homework? Smells like homework.

The most noticeable difference is that FO can have state and keep it
between uses. TFs essentially cannot. That makes it possible to provide
additional "arguments" to the operation which they are to perform without
changing the interface of the actual caller.
 
R

richardclay09

Thanks - I am definitely too old for it to be homework though. What I
was trying to understand was this piece of text from
http://pages.cpsc.ucalgary.ca/~kremer/. Please see the question I have
marked ==>> below:

BEGIN QUOTE:

Function Objects work by overloading operator().

Example:
#include < iostream.h >

template < class T >
struct square {
T operator()(T n) {return n*n;}
};

template < class T, class funcObj >
void printEval(T val,funcObj f) {
cout << f(val) << endl;
};

main() {
printEval(1.4, square < float > ());
printEval(1.4, square < int > ());
return 0;
}

output:

1.96
1

Very useful for generating needed functions on-the-fly. (Can't use
template functions for this since we can't specify the template
arguments.)
END QUOTE
==>> I must be in thick mode tonight, I can't get my head round the bit
in brackets ("Can't use.......arguments"). Too much red wine....
 
V

Victor Bazarov

Thanks - I am definitely too old for it to be homework though. What I
was trying to understand was this piece of text from
http://pages.cpsc.ucalgary.ca/~kremer/. Please see the question I have
marked ==>> below:

BEGIN QUOTE:

Function Objects work by overloading operator().

Example:
#include < iostream.h >


template < class T >
struct square {
T operator()(T n) {return n*n;}
};

template < class T, class funcObj >
void printEval(T val,funcObj f) {
cout << f(val) << endl;
};

main() {

int main() {
printEval(1.4, square < float > ());
printEval(1.4, square < int > ());
return 0;
}

output:

1.96
1

Very useful for generating needed functions on-the-fly. (Can't use
template functions for this since we can't specify the template
arguments.)
Huh?

END QUOTE
==>> I must be in thick mode tonight, I can't get my head round the bit
in brackets ("Can't use.......arguments"). Too much red wine....

I think the author you're quoting is simply wrong here. It is possible
he/she is using some hopelessly outdated compiler. Here is the same code
with the function 'square' instead of a type:
-----------------------------
#include <iostream>

template < class T > T square(T n) { return n*n; }

template < class T, class funcObj >
void printEval(T val,funcObj f) {
std::cout << f(val) << std::endl;
};

int main() {
printEval(1.4, square < float >);
printEval(1.4, square < int >);
return 0;
}
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top