【问题标题】:How to get the the signature type from the function pointer type?如何从函数指针类型中获取签名类型?
【发布时间】:2023-01-20 20:07:29
【问题描述】:

假设我有以下函数指针 typedef:

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

如何使用FType 的签名构造一个std::function 对象?

例如,如果 FType 是使用 using FType = int(int,int) 定义的,则可以通过 std::funtion<FType> func = ... 完成

【问题讨论】:

    标签: c++ c++17


    【解决方案1】:
    using FType = int(*)(int,int);
    std::function<std::remove_pointer_t<FType>> func;
    

    【讨论】:

      【解决方案2】:

      std::function 可以做 CTAD,因此这个有效:

      #include <iostream>
      #include <functional>
      
      using FType = int(*)(int,int);
      
      int foo(int,int) {}
      
      int main(){
          FType x = &foo;
          auto f = std::function(x);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-04
        • 2019-12-22
        • 2021-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多