开发者

C++ Function Pointer as Default Template Argument

开发者 https://www.devze.com 2023-01-31 05:29 出处:网络
how do I write a function pointer as default template argument, I\'am guessing to write it like this:

how do I write a function pointer as default template argument, I'am guessing to write it like this:

template<typename R,
         typename A,
         typename F=R (*PF)(A)> 
class FunctionPtr { ...

my question is,

1.is开发者_开发百科 it possible?

2.if it is and my guess code above is correct, what the purpose of PF here? do I need this?


  1. Yes
  2. No, you don't need PF.

    template<typename R,
             typename A,
             typename F=R (*)(A)> 
    


  1. Yes, it is possible
  2. The PF not only is useless but must be removed in this context. It would, for example, be necessary in the context of a function pointer declaration :

    int (*PF)(double) = &A::foo; // declares a 'PF' variable of type 'int (*)(double)'
    

    but it is neither required nor legal here.


I suggest typedefing pointer to function. typedef R (*PF)(A); then give PF as a default template argument.

0

精彩评论

暂无评论...
验证码 换一张
取 消