Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to pass static function as template argument without adding new function argument?

One can pass callback to other function using template to abstract from real callback type:

float foo(int x, int y)  {return x*y;}

template<class F>
void call_it(F f, int a, int b)
{
    f(a,b);
}

There is a cost of passing f as an argument and calling it indirectly. I wonder if, in case f is a static function it is possible to pass it somehow to template function "directly", without adding a callable to the function argument list, so that the call could be bound statically.
I see the analogy to passing an integer value as template argument. It doesn’t require adding any new function arguments because it passes the integer just as immediate value into the function code:

template<int X> int foo(int y) {return X+y;}

Here is a non-working code presenting what I’d like to achieve:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

template<class F>
void call_it(int a, int b)
{
    F(a,b);    // Assume that F is a static function and can be called directly
}

Is there any way to achieve it?

>Solution :

You can use a non-type template parameter. To still allow all kinds of callables you can use auto:

#include <iostream>

struct foo {
    static float bar(int a,int b){
        std::cout << "foo: " << a << " " << b;
        return a + b;
    }
};

template <auto f>
float call_it(int a,int b){
    return f(a,b);
}

int main() {
    call_it<&foo::bar>(1,2);
}

or without auto:

template <float (*f)(int,int)>
float call_it(int a,int b){
    return f(a,b);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading