Functors

Hello,
I would like to use a functor which is also calling an other functor. I am not sure if I am using the right terminology. I manage to use only one functor but not a nested one. I have the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

template<typename T>
class Prueba {
public:
        template<typename Function, typename Normalization >
        static void usesnormalize (Function lafuncion, Normalization normalize, T a) {
                lafuncion (normalize, a);
        }

        template<typename Normalization>
        static void lafuncion1 (Normalization normalize, T a) {
                normalize (a);
        }

        static void normalize1 (T a) {
                cout << "normalize1. " << endl;
        }

        static void normalize2 (T a) {
                cout << "normalize2. " << endl;
        }

};

int main (int argc, char* argv[] ) {

        int b;
        Prueba<int>::usesnormalize (Prueba<int>::lafuncion1, Prueba<int>::normalize2, b );
        int a;
        Prueba<int>::lafuncion1 (Prueba<int>::normalize1, a );
}


I am getting the following error:
functiontemplate.cpp: In function ‘int main(int, char**)’:
functiontemplate.cpp:31: error: no matching function for call to ‘Prueba<int>::usesnormalize(<unresolved overloaded function type>, void (&)(int), int&)’

Thanks in advance,
Sergio.
You need to specify the template parameter
Prueba<int>::usesnormalize (Prueba<int>::lafuncion1<void (*)(int)>, Prueba<int>::normalize2, b );
Horrible, don't you think?
Last edited on
Indeed :) but it worked, thanks!
What you've written above is not a functor.

The correct definition of a functor is an object that creates function objects.

A function object is an object that implements the function call operator.

Note that many people use the term "functor" when they really mean "function object".
jsmith wrote:
The correct definition of a functor is an object that creates function objects.


I had not heard that definition before. I learned that a functor is a class whose only method is the function operator (operator()).

In this case, the OP uses the term "functor" to mean "static member function".

http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.15

A functor is a special case of a functionoid: it is a functionoid whose method is the "function-call operator," operator()().


Is that wrong?
My code can also be written by using global functions instead of static member functions. I think I meant by functor a function object, as it is explained in:

http://en.wikipedia.org/wiki/Function_object#In_C_and_C.2B.2B

In addition to class type functors, other kinds of function objects are also possible in C++. They can take advantage of C++'s member-pointer or template facilities. The expressiveness of templates allows some functional programming techniques to be used, such as defining function objects in terms of other function objects (like function composition). Much of the C++ Standard Template Library (STL) makes heavy use of template-based function objects.


Apparently, functors are a kind of function objects.
Topic archived. No new replies allowed.