Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ functor and function templates

consider this simple and pointless code.

#include <iostream>

struct A {
    template<int N>
    void test() {
        std::cout << N << std::endl;
    }
};

int main() {
    A a;
    a.test<1>();
}

It is a very simple example of a function template. What if however, I wanted to replace A::test with an overloaded operator() to make it a functor?

#include <iostream>

struct A {
    template<int N>
    void operator()() {
        std::cout << N << std::endl;
    }
};

int main() {
    A a;
    a<1>(); // <-- error, how do I do this?
}

Certainly if the operator() took parameters which were dependent on the template, the compiler could possibly deduce the template. But I just can't figure out the proper syntax to specify template parameters with a parameterless functor.

Is there a proper way to do this?

Obviously, this code would work since it bypasses the functor syntax:

a.operator()<1>();

but that kinda defeats the purpose of it being a functor :-P.

like image 898
Evan Teran Avatar asked Jun 02 '09 22:06

Evan Teran


People also ask

What is a function template in C?

Templates Specialization is defined as a mechanism that allows any programmer to use types as parameters for a class or a function. A function/class defined using the template is called a generic function/class, and the ability to use and create generic functions/classes is one of the critical features of C++.

What is the difference between a function and a functor?

A function assigns to every element of a set X an element of a set Y. A functor assigns to every object of a category C an object of a category D and also assigns to every morphism in C a morphism in D in a way compatible with sources, targets, and composition.

What is the difference between a functor and a function pointer?

A function pointer allows a pointer to a function to be passed as a parameter to another function. Function Objects (Functors) - C++ allows the function call operator() to be overloaded, such that an object instantiated from a class can be "called" like a function.

What is a function template?

Function templates are similar to class templates but define a family of functions. With function templates, you can specify a set of functions that are based on the same code but act on different types or classes. The following function template swaps two items: C++ Copy.


2 Answers

You can only call

a.operator()<1>();

but that would not be using a functor. Functors need a non template operator(), as they must be able to be called as varname() and that won't work with your code.

To make it a real functor change your code a template class (functors are classes):

#include <iostream>

template<int N>
struct A {
    void operator()() {
        std::cout << N << std::endl;
    }
};

int main() {
    A<1> a;
    a();
}
like image 99
lothar Avatar answered Sep 21 '22 07:09

lothar


There's not another "direct" way I know other than the:

 a.operator()<1>();

syntax. If you're open to changing the code, moving the template parameter to the class would work, or using a (boost|tr1)::bind to make a (boost|tr1)::function object.

like image 45
Todd Gardner Avatar answered Sep 24 '22 07:09

Todd Gardner