LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2011, 07:36 AM   #1
sd9
Member
 
Registered: Apr 2009
Posts: 53

Rep: Reputation: 15
Difficulty with function objects


For this code I found on Wikipedia;
Code:
class compare_class {
  public:
  bool operator()(int A, int B) const {
    return A < B;
  }
};
...
// Declaration of C++ sorting function.
template <class ComparisonFunctor> 
void sort_ints(int* begin_items, int num_items, ComparisonFunctor c);
...
int main() {
    int items[] = {4, 3, 1, 2};
    compare_class functor;
    sort_ints(items, sizeof(items)/sizeof(items[0]), functor);
}
I don't understand how the "functor" function here
Code:
sort_ints(items, sizeof(items)/sizeof(items[0]), functor);
receives the two parameters "int A, int B".

I would've expected something like
Code:
sort_ints(items, sizeof(items)/sizeof(items[0]), functor(items, sizeof(items)/sizeof(items[0]) ));
, where "functor" is passed two values as how it is shown in the class definition here:
Code:
bool operator()(int A, int B)
How do the values get passed to "functor" automatically?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-29-2011, 08:16 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by sd9 View Post
I would've expected something like
Code:
sort_ints(items, sizeof(items)/sizeof(items[0]), functor(items, sizeof(items)/sizeof(items[0]) ));
That is very confused, because you are passing the wrong kind of things to the functor. It is used for comparing two specific items, not for comparing the pointer to all items against the count of items !?

Quote:
How do the values get passed to "functor" automatically?
functor is called inside the source code of sort_ints.

That is only "automatic" if you consider sort_ints to be a black box, rather than part of your code.

Consider this code:
Code:
sort_ints( items, count, functor(x,y));
Ignoring for the moment what x,y might be, we can still notice that functor is called before sort_ints is called and the result returned by functor is the third parameter passed to sort_ints. sort_ints can't use functor multiple times. It only gets the use of one return value from functor done before sort_ints even starts.

Contrast that with:
Code:
sort_ints( items, count, functor);
There functor is not called before sort_ints. functor itself, rather than the result of one call to functor, is passed as the third parameter. Inside sort_ints, functor may be used multiple times. sort_ints provides the parameters for each call to functor and uses the results.

Last edited by johnsfine; 01-29-2011 at 05:46 PM.
 
1 members found this post helpful.
Old 01-29-2011, 12:06 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Though probably off-topic, but C++ standard claims functions are not objects, so the thread name is confusing.
 
1 members found this post helpful.
Old 01-29-2011, 04:34 PM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by Sergei Steshenko View Post
Though probably off-topic, but C++ standard claims functions are not objects, so the thread name is confusing.
Function Object == Functor.

Or you can call it "Whoop-tee-doo" if you wish. But "Function Object" and "Functor" are names well known within the C++ community.
 
2 members found this post helpful.
Old 01-29-2011, 04:43 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
Though probably off-topic, but C++ standard claims functions are not objects, so the thread name is confusing.
That's why passing a function to this type of template usually requires it to handle a pointers. The example code probably doesn't, though.

For a template to take both a functor and a function interchangeably, it should either assume that the argument is a pointer or it should use partial specialization to deduce whether it's a function pointer, a function reference, an object pointer, an object reference, or an object passed by value. In general, a function like this is used:
Code:
template <class Type, class Function>
bool compare(const Type &lLeft, const Type &rRight, Function *fFunction)
{ return (*fFunction)(lLeft, rRight); }
When either a function pointer or object pointer are passed, the template makes the assumption that () with two arguments can be called on whatever it points to. In the case of a functor, this is done by defining operator (). The key thing to remember is that a template is instantiated during the first phase of compilation; an "actual" version of the template is created depending on how you call it, which is then compiled.

You should also look at http://www.newty.de/fpt/index.html, previously function-pointer.org.
Kevin Barry
 
2 members found this post helpful.
Old 01-29-2011, 04:44 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by dwhitney67 View Post
Function Object == Functor.

Or you can call it "Whoop-tee-doo" if you wish. But "Function Object" and "Functor" are names well known within the C++ community.
I have just checked the word 'functor' in 'C++ Standard - ANSI ISO IEC 14882 2003.pdf' - no matches. But "function object" is present.
 
2 members found this post helpful.
Old 01-30-2011, 11:57 PM   #7
sd9
Member
 
Registered: Apr 2009
Posts: 53

Original Poster
Rep: Reputation: 15
thanks everyone!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
how to call function inside c shared objects from a php extensions eilaf Linux - Server 1 10-21-2008 02:00 AM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration