Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to typedef template function pointer?

I want to typedef a function pointer that points to a template function.

class A
{
    template <typename T>
    typedef void (*FPTR)<T>();
}

I have tried in this way and didn't succeed. Any idea about this thing?

like image 653
arsdever Avatar asked Aug 26 '26 08:08

arsdever


1 Answers

As @HolyBlackCat pointed out, the normal function pointer should work as you have a simple templated void function, whose template parameter does not act on both return and argument types.

template <typename T>
void someVoidFunction() {}

using fPtrType = void(*)();

int main()
{
    fPtrType funPtr1 = &someVoidFunction<int>;
    fPtrType funPtr2 = &someVoidFunction<float>;
    fPtrType funPtr3 = &someVoidFunction<std::string>;
    return 0;
}

If it was the case, that template parameters depends on the function arg and return types you should have instantiated the function pointer as well for each kind.

template <typename T, typename U>
T someFunction(U u) {}

template <typename T, typename U>
using fPtrType = T(*)(U);

int main()
{
    fPtrType<int, float> funPtr1 = &someFunction<int, float>;  // instance 1
    fPtrType<float, float> funPtr2 = &someFunction<float, float>; // instance 2
    return 0;
}
like image 92
JeJo Avatar answered Aug 28 '26 21:08

JeJo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!