Home > Software engineering >  Template function as argument in a class
Template function as argument in a class

Time:04-08

I wanna pass a function argument in the constructor of the following class:

template <class T>
class Class
{
private:
    bool (*fc)(T, T);
public:
    template <class T>
    Class(const bool(*func)(T, T))
    {

    }
    ~Class() {}
};

bool randomFunction(int a, int b)
{
    return a <= b;
}


int main() {
    LDI<int> test(randomFunction);
    return 0;
}

Severity Code Description Project File Line Suppression State Error C2664 'Class::Class(const bool (__cdecl *)(T,T))': cannot convert argument 1 from 'bool (__cdecl *)(int,int)' to 'const bool (__cdecl *)(T,T)

Error (active) E0289 no instance of constructor "Class::Class[with T=int]" matches the argument list

How do I fix it and where's the problem?

CodePudding user response:

Remove the template part before the constructor, and use a std::function to simplify your code.

#include <functional>

template <class T> class LDI
{
public:
    // Define this type using std::function, so it can accept function, lambdas, ... very easily.
    // If used with std::bind, can also accept pointers to members functions.
    typedef std::function<bool(T,T)> tmplfunc ;
private:
    tmplfunc fc ;
public:
    // No template here, class is already a template.
    LDI(tmplfunc aFc)
    {
        fc = aFc ;
    }
    ~LDI() {}
};

bool randomFunction(int a, int b)
{
    return a <= b;
}

int main() {
    LDI<int> test(randomFunction);
    LDI<double> test2([](double a, double b) -> bool { return a<=b ;});
    return 0;
}

CodePudding user response:

If you're ok with using #include <functional>, here is an example of defining a class that has a function as a variable-style member provided through the constructor. Although I wouldn't recommend the current implementation, geeksforgeeks has a good resource on functions as arguments that can be applied to your constructor.

  • Related