473,396 Members | 1,671 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,396 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 1404
> 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 member functions and came across a simple problem...
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. Essentially at the point of the error, we invoke an...
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 different objects of different type (they are all the...
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 FuncT> // <- What would go here class CObjectT {
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 presented in _Modern C++ Design_ for message passing in...
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 luck :) The standard explanation is that pointer...
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 template function , where EntryType is the input for...
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 // -------------------------------------- class Interface {...
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 C++? I'd like to replace double (*fun)(double)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.