C++: Function pointer as Template argument instead of functor

SumitV

I have been trying to create this class which can either use the default functor as an argument or the user can provide one if he wants. But I am unable to pass function pointer as my template argument. Can you please help me in understanding what I am missing.

template <typename T>
struct CheckFunctor
{
    bool operator()(T obj)
    {
        return true;
    }
};



template <typename _Ty,
class _Pr = CheckFunctor<_Ty>
>
class MyClass
{
    typedef _Ty                     mapped_type;
    typedef _Pr                     CanBeCleaned_type;

    _Ty data;
    CanBeCleaned_type predicate;

public:  

    void SomeMethod()
    {
            if( predicate(data))
            {
              std::cout << "Do something";
            }
    }
       MyClass(_Ty timeOutDuration, _Pr pred = _Pr())
        : data( timeOutDuration), predicate( pred)
    {}   
};

template< typename T>
struct CheckEvenFunctor
{
   bool operator()(T val)
    {
       return (val%2 == 0);
    }
};


bool CheckEven( int val)
{
    return (val%2 == 0);
}

int main()
{
//Usage -1
    MyClass<int> obj1( 5);

//Usage- 2
 MyClass< int, CheckEven> obj2(6, CheckEven);  //Error: 'CheckEven' is not a valid template type argument for parameter '_Pr'

 //Usage -3 
 MyClass<int, CheckEvenFunctor<int>>( 7);
}
eerorika

You are trying to pass CheckEven as a type parameter even though CheckEven is not a type but a function (of type bool(int)). You should define the type as a pointer to the type of function that you are passing. decltype is handy here:

MyClass< int, decltype(&CheckEven)> obj2(6, CheckEven);

You can also create a factory function and let the compiler deduce the template parameters:

template<class T, class F>
MyClass<T, F> makeClass(T timeOutDuration, F pred) {
    return {timeOutDuration, pred};
}

auto obj2 = makeClass(6, CheckEven);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++: Function pointer as Template argument instead of functor

From Dev

Why is C++ lambda implemented with functor instead of function pointer?

From Dev

Function pointer as template argument and signature

From Dev

decltype of member function pointer as template argument in c++11

From Dev

decltype of member function pointer as template argument in c++11

From Dev

Casting function pointer to functor in C++

From Dev

C++ struct functor as function template parameter

From Dev

Function pointer, Functor or Lambda?

From Dev

function pointer to functor

From Dev

function pointer to functor

From Dev

Functor and function pointer logic

From Dev

Function pointer, Functor or Lambda?

From Dev

Calling a zero argument template function pointer with variadic template argument?

From Dev

Function template overload resolution with a pointer argument

From Dev

Passing pointer to member function parameter into template argument

From Dev

function template deduction using raw pointer as argument

From Dev

C pass pointer as argument in function

From Dev

c++ pointer to a function as argument

From Dev

c++ pointer to a function as argument

From Dev

passing function pointer in c with a pointer argument

From Dev

c++: function pointer with template

From Dev

C++ function pointer as template

From Dev

C++ function pointer in template

From Java

C++20 'familiar template' lambdas: specifying an explicit argument in function pointer conversion

From Dev

C++ - Passing a function pointer taking const argument to a template class is wrongly interpeted

From Dev

How to assign functor to function pointer?

From Dev

Template function pointer of template class - C++

From Dev

Function returning <error-type> instead of the intended raw pointer to a template class in C++

From Dev

Deducing template parameter from functor argument type

Related Related

  1. 1

    C++: Function pointer as Template argument instead of functor

  2. 2

    Why is C++ lambda implemented with functor instead of function pointer?

  3. 3

    Function pointer as template argument and signature

  4. 4

    decltype of member function pointer as template argument in c++11

  5. 5

    decltype of member function pointer as template argument in c++11

  6. 6

    Casting function pointer to functor in C++

  7. 7

    C++ struct functor as function template parameter

  8. 8

    Function pointer, Functor or Lambda?

  9. 9

    function pointer to functor

  10. 10

    function pointer to functor

  11. 11

    Functor and function pointer logic

  12. 12

    Function pointer, Functor or Lambda?

  13. 13

    Calling a zero argument template function pointer with variadic template argument?

  14. 14

    Function template overload resolution with a pointer argument

  15. 15

    Passing pointer to member function parameter into template argument

  16. 16

    function template deduction using raw pointer as argument

  17. 17

    C pass pointer as argument in function

  18. 18

    c++ pointer to a function as argument

  19. 19

    c++ pointer to a function as argument

  20. 20

    passing function pointer in c with a pointer argument

  21. 21

    c++: function pointer with template

  22. 22

    C++ function pointer as template

  23. 23

    C++ function pointer in template

  24. 24

    C++20 'familiar template' lambdas: specifying an explicit argument in function pointer conversion

  25. 25

    C++ - Passing a function pointer taking const argument to a template class is wrongly interpeted

  26. 26

    How to assign functor to function pointer?

  27. 27

    Template function pointer of template class - C++

  28. 28

    Function returning <error-type> instead of the intended raw pointer to a template class in C++

  29. 29

    Deducing template parameter from functor argument type

HotTag

Archive