15

I came across a function with this signature.

void foo(char (&x)[5])
{
}

This is the syntax for passing a fixed size char array by reference.

The fact that it requires parentheses around &x strikes me as unusual.

It is probably part of the C++03 standard.

What is this form called and can anyone point out a reference to the standard?

c++decl is not a friend yet:

$ c++decl 
Type `help' or `?' for help
c++decl> explain void foo(char (&x)[5])
syntax error
3
  • 3
    I can't point to the standard, but I believe it's because char& x[5] is an array of 5 character references, not a reference to a character array of length 5. Commented Oct 26, 2011 at 12:20
  • 1
    8.3.2 References "There shall be no references to references, no arrays of references, and no pointers to references." Commented Oct 26, 2011 at 12:33
  • 1
    Wasn't it a really great call to use punctuation for such features. Commented Oct 26, 2011 at 12:52

3 Answers 3

12

There is nothing unusual or new about the syntax. You see it all the time in C with pointers. [] has higher precedence than &, so you need to put it in parentheses if you want to declare a reference to an array. The same thing happens with * (which has the same precedence as &): For example, to declare a pointer to an array of 5 chars in C, you would do char (*x)[5];. Similarly, a pointer to a function that takes and returns an int would be int (*x)(int); (() has same precedence as []). The story is the same with references, except that references are only on C++ and there are some restrictions to what types can be formed from references.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 For explaining the requirement of the parentheses. Where are the restrictions on forming types from references documented?
@Eddy Pronk: I was referring to the statement you cited in your comment to your question above
11

There's nothing to explain, this is simply how the parsing rules for declarations work in C++:

char  & x[5] // declare x as array 5 of reference to char (not valid C++!)
char (&x)[5] // declare x as reference to array 5 of char

Caution: The first version is not valid C++, though, since you cannot have arrays of references. This is merely an explanation of the declaration syntax. (Sorry about taking so long to get this right, and thanks to the helpful comments!)

You're allowed to wrap the type identifier in arbitray levels of parentheses if you like, so you can also say char &(x)[5] (first case) or char (((&x)))[5] (second case).

10 Comments

Which compiler accepts the first form? gcc 4.4.5 doesn't: error: declaration of ‘x’ as array of references
Except, of course, that the first isn't legal C++, since you're not allowed to have arrays of references.
It seems you're required to wrap at least one level of parentheses.
@JamesKanze: Not as free-standing declarations, but as function argument declarations! Good point :-)
Compare void (*funcptr)(int) to declare a function pointer as opposed to a function which returns a pointer.
|
0

c++decl mostly works for this. It's just a bit picky about what you give it.

c++decl> explain void foo(char (&)[5])
declare foo as function (reference to array 5 of char) returning void

c++decl> explain void foo(char &[5])
declare foo as function (array 5 of reference to char) returning void

As noted in another answer, an array of references is illegal. The GNU C++ compiler reports:

error: declaration of ‘x’ as array of references

By the way, here is a link to an online utility that serves cdecl (although it complains about references because the version hosted by the site is C-specific).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.