How to typedef template function pointer?

Viewed 2333

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?

2 Answers

Template functions produce functions. Template classes produce classes. Template variables produce variables.

Pointers can point at functions or variables. They cannot point at templates; templates have no address.

Typedef defines the type of a variable.

A template variable pointer could collectively point at various instances of a template function, but the initial binding would be done at compile time, and could only be aimed somewhere else one variable at a time.

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;
}
Related