472,340 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Template functions vs. function objects

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;
}

Jul 23 '05 #1
4 1336
> 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.
Jul 23 '05 #2
ri***********@yahoo.co.uk wrote:
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.
[...]

Jul 23 '05 #3
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....

Jul 23 '05 #4
ri***********@yahoo.co.uk wrote:
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 >
#include <iostream>
using namespace std;

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;
}
-----------------------------

Victor
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. ...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of...
6
by: Brian Ross | last post by:
Hi, I am trying to do something similar to the following: int Func1(int x, int y); double Func2(double x, double y); template <typename...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better...
2
by: Nike | last post by:
I have a small question w.r.t usage of default arguements in template.. I shall try to elaborate this with an example.. let's say I have some...
8
by: Belebele | last post by:
Suppose that I want to write a (concrete) interface class (without virtual functions) to classes that contain a method template //...
5
by: Szabolcs | last post by:
I am looking for a way to generalize the function template below, so that it will work with any type, not just double. Is this at all possible in...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.