3

What is the difference between below syntax:

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

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

I want to know the practical use of the 2nd syntax apart from libraries (I checked that we cannot declare object of void() inside A). I have referred this question, but that din't help.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336

1 Answers1

1

void() is the type of a function taking no arguments, and returning nothing.

void(*)() is the type of a pointer to a function taking no arguments, and returning nothing.

As an example of where void() is used and is useful, look at std::function -- the syntax it uses is much nicer than if you had to pass in a function pointer signature. You can use the exact same syntax when you mean "I want to tell this template class the signature of a call".

Mainly, this is just syntactic sugar. But sugar is the spice of life.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524