LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Difficulty with function objects (https://www.linuxquestions.org/questions/programming-9/difficulty-with-function-objects-859402/)

sd9 01-29-2011 07:36 AM

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?

johnsfine 01-29-2011 08:16 AM

Quote:

Originally Posted by sd9 (Post 4241453)
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.

Sergei Steshenko 01-29-2011 12:06 PM

Though probably off-topic, but C++ standard claims functions are not objects, so the thread name is confusing.

dwhitney67 01-29-2011 04:34 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4241649)
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.

ta0kira 01-29-2011 04:43 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4241649)
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

Sergei Steshenko 01-29-2011 04:44 PM

Quote:

Originally Posted by dwhitney67 (Post 4241831)
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.

sd9 01-30-2011 11:57 PM

thanks everyone! :)


All times are GMT -5. The time now is 07:52 PM.