Difference between function pointer as template argument and function signature as template argument

What is the difference between the syntax below:

template<typename T>
struct A { ... };

A<void (*)()> o1; // <--- ok
A<void()> o2;  // <----- ??

      

I want to know the practical use of the second syntax besides libraries (I verified that we cannot declare an object void()

inside A

). I called this question , but it doesn't help.

+3


source to share


1 answer


void()

is the type of a function that takes no arguments and returns nothing.

void(*)()

is the type of a pointer to a function that takes no arguments and returns nothing.



For an example where it is void()

used and useful, have a look at std::function

- the syntax it uses is much nicer than if you had to pass a function pointer signature. You can use the same syntax when you mean "I want to tell this template class to sign the call."

Basically, it's just syntactic sugar. But sugar is the spice of life.

+1


source







All Articles