Jonathan Mee
Jonathan Mee

Reputation: 38919

Are Parameter Names Allowed in Function Pointer Deffinitions

So I just saw an example of a function pointer declaration that uses named parameters:

int (*func)(int a, int b) = &foo;

I'm assuming that this is well defined code cause it compiles and runs fine: https://ideone.com/C4mhsI

But I've never seen this before (probably because it increases the complexity of already difficult to parse function pointers.) Nonetheless, I'd like to see a statement that this is legal and not some crazy gcc extension. Can someone find me something?

Upvotes: 2

Views: 159

Answers (2)

Rakete1111
Rakete1111

Reputation: 48938

You can follow the grammar of a declarator, which is a declaration without any type or decl specifiers.

declarator:
  ptr-declarator
  [...]

ptr-declarator:
  noptr-declarator
  ptr-operator ptr-declarator

noptr-declarator:
  [...]
  noptr-declarator parameters-and-qualifiers
  ( ptr-declarator )

parameters-and-qualifiers:
  ( parameter-declaration-clause ) [...]

ptr-operator:
  * attribute-specifier-seq[opt] cv-qualifier-seq[opt]
  [...]

I removed every grammar rule that isn't needed, to be able to follow it more easily. I won't do it because it's a bit of a pain to write down, but at the end you will find that you have parameters-and-qualifiers that represents the parameters.

And if you do it for a function, you will see that it also ends with parameters-and-qualifiers. They use the same grammar, and there is nothing prohibiting this in the standard under [decl.ptr].

Upvotes: 6

mdatsev
mdatsev

Reputation: 3879

Just like you can you can specify the argument name in a function declaration: int funct(int a, int b); you can in function pointers too int (*func)(int a, int b). In both cases the names don't do anything, and in practice they are used as a documentation aid, for easier reading.

Upvotes: -1

Related Questions