wildcard function pointer non-type template parameter

前端 未结 2 1982
清酒与你
清酒与你 2021-01-28 11:34

Templates can take non-type function pointer parameters, but there is a problem if all possible function pointer parameters are accepted, example:

void dummy()
{         


        
相关标签:
2条回答
  • 2021-01-28 12:11

    If you know the signature of the function you wish to call (and surely you do), can't you do

    template<void F()>
    void proxy()
    {
        F();
    }
    
    void dummy() {}
    
    int main()
    {
        proxy<dummy>();
    }
    
    0 讨论(0)
  • 2021-01-28 12:26

    A better solution for your particular problem would be to simply take only the function type as a template argument and the item as an ordinary function argument. You can also use type deduction instead of explicitly specifying which argument types are used:

    void dummy()
    {
    }
    
    template <typename FT>
    void proxy(FT fp)
    {
      fp();
    }
    
    int main()
    {
      proxy(fp);
    
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题