Home > other >  How to get the the signature type from the function pointer type?
How to get the the signature type from the function pointer type?

Time:01-20

Say I have the following function pointer typedef:

    using FType = int(*)(int,int);

How can I construct a std::function object using the signature of FType?

For example if FType were defined using using FType = int(int,int), it could be done by std::funtion<FType> func = ...

CodePudding user response:

using FType = int(*)(int,int);
std::function<std::remove_pointer_t<FType>> func;

CodePudding user response:

std::function can do CTAD, hence this works:

#include <iostream>
#include <functional>

using FType = int(*)(int,int);

int foo(int,int) {}

int main(){
    FType x = &foo;
    auto f = std::function(x);
}
  • Related