ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.