6

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?

3 Answers 3

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

    template<typename R,
             typename A,
             typename F=R (*)(A)> 
    
Sign up to request clarification or add additional context in comments.

4 Comments

more question, how do i write for member function, like this? template<typename C,typename R,typename A, typename F=R (C::*)(A)>
got err. C2653 on VC'03 : " 'C' is not a class or namespace " on typename F=R (C::*)(A)>
I'am asked another question here : stackoverflow.com/questions/4450246/…
@uray: How do you instantiate this? C has to be a class-type.
2
  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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.