0

If I have a function...

void f(int x[3]) {}

...is this distinguishable from a function...

void f(int* x) {}

If not, where in the standard does it address this issue?

Clearly there are array-to-pointer standard conversions, but I don't think they apply here?

I seem to remember language which said something to this effect, but can't seem to find it.

$ cat t.cc
void f(int x[3]) {}

void f(int* x) {}

$ g++ t.cc
t.cc: In function ‘void f(int*)’:
t.cc:3:6: error: redefinition of ‘void f(int*)’
 void f(int* x) {}
      ^
t.cc:1:6: note: ‘void f(int*)’ previously defined here
 void f(int x[3]) {}
      ^
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319

3 Answers3

4

If I have a function...

void f(int x[3]) {}

...is this distinguishable from a function...

void f(int* x) {}

No. Those declarations are equivalent.

Standard draft:

[dcl.fct]

The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”. ...

This rule complements the array decaying rule. Array decaying allows calling a function with - apparently - an array argument, while this rule allows declaring a function with - apparently - an array argument, while actually what is passed is a pointer. These rules stem from the C heritage of the language. Same with function to pointer decaying.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • `[dcl.fct]/5` : http://eel.is/c++draft/dcl.fct#5 – Andrew Tomazos Nov 20 '18 at 14:46
  • @AndrewTomazos note that the numbering of § paragraphs often change between standard versions, which is why I avoid them in my anwers. Sometimes rules migrate under different named sections too, but that's more uncommon. – eerorika Nov 20 '18 at 14:50
3

From [dcl.fct§5] (emphasis mine):

The type of a function is determined using the following rules. The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T.

Quentin
  • 62,093
  • 7
  • 131
  • 191
2

It's described in the specification of function declarators

[dcl.fct]

5 [...] After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”. [...]

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458