-1

The code of std::function in gcc has these two lines:

template<typename _Res, typename... _ArgTypes>
  class function<_Res(_ArgTypes...)> // <-- unclear to me

The first part template... _ArgTypes denotes a "parameter pack", i.e., a variadic number of template parameters; that is clear. But the second line is magic. OK, writing class function<SmthHere> means template specialization, so we specialize class function with _Res(_ArgTypes...). The latter looks like a function call with a variable number of arguments. However if _Res is void and _ArgTypes is int, we get void(int): this doesn't make sense to me as we can't have a function named void and pass an argument int to it (??). Is this a specially supported syntax? Could you clarify?

Ayrat
  • 1,221
  • 1
  • 18
  • 36
  • 1
    Not that sure, what you actually mean with _"as we can't have a function called `void` and moreover pass to it an argument `int` (??)."_: https://coliru.stacked-crooked.com/a/46669ef2aa24e233 – πάντα ῥεῖ Nov 05 '21 at 21:09

1 Answers1

3

I think you are confused about the declaration syntax (which is inherited from C language): here, the syntax void(int) does not mean that a function named void and taking an argument named int is being called. Instead, it denotes a type, which is a function, taking a parameter of type int and returning void.

You can read more about function declaration syntax at C++ Reference, there are some examples as well.

heap underrun
  • 1,846
  • 1
  • 18
  • 22
  • 1
    I see now, the idea is: at first, the class `function` is declared as a template with parameter `signature`. Then later two additional template parameters `_Res` and `_ArgTypes` are introduced and the class is defined using `function<_Res(_ArgTypes)>`, and _the key_ is, as you wrote, that `_Res(_ArgTypes)` is actually a type (an existing C syntax for function types). I've seen things like `void (*foo)(int)` but haven't realized it means that `foo` points to a function of type `void(int)`. Thanks! – Ayrat Nov 10 '21 at 08:16