views:

414

answers:

3

Hi all, I was reading litb's question about SFINAE here and I was wondering exactly what his code is declaring. A simpler (without the templates) example is below:

int (&a())[2];

What exactly is that declaring? What is the role of the &? To add to my confusion, if I declare the following instead

int b()[2];

I get an error about declaring a function that returns an array, while the first line has no such error (therefore, one would think the first declaration is not a function). However, if I try to assign a

a = a;

I get an error saying I'm attempting to assign the function a... so now it is a function. What exactly is this thing?

Thanks in advance

+16  A: 

There's these awesome programs called cdecl and c++decl. They're very helpful for figuring out complicated declarations, especially for the byzantine forms that C and C++ use for function pointers.

tyler@kusari ~ $ c++decl
Type `help' or `?' for help
c++decl> explain int (&a())[2]
declare a as function returning reference to array 2 of int
c++decl> explain int b()[2]
declare b as function returning array 2 of int

a returns a reference, b does not.

Tyler McHenry
wow, those programs are useful. I don't suppose a Windows port exists?
jalf
I don't know of a canonical version of cdecl for Windows (although of course you could always use the Linux version through Cygwin), but if you google for "cdecl.zip" it seems to pull up a few windows ports that various people have done.
Tyler McHenry
That is actually very useful.
Martin York
As far as I can see, the .zip dates back to 1987, which means it's probably not very useful for C++ syntax... :)But thanks, I'll check it out. Or just use it through Cygwin
jalf
cdecl is now a webapp: http://www.cdecl.org/
David
ran into those programs a while ago. legendary!
Matt Joiner
@jalf: c++ on winders? cast away your shackles!
Matt Joiner
@Matt: I love when people claim that the way to "cast away your shackles" is to limit your options. Because yes, I'm so much more free and unshackled if I target Linux than I am if I target both Linux and Windows. ;)
jalf
+1  A: 
int (&a())[2];

It declares a symbol a that is a function that takes no arguments and returns a reference to a two-element array of integers.

 int b()[2];

This declares a symbol b that is a function that takes no arguments and returns a two-element array of integers... this is impossible by the design of the language.

It is relatively simple: get an operator precedence chart, start the symbol name (a) and start applying the operators as you see from their precedence. Write down after each operation applied.

Juliano
+8  A: 

For future reference, you may find this link helpful when you have a particularly difficult C/C++ declaration to decipher:

How To Read C Declarations

For completeness, I will repeat what others have said to directly answer your question.

int (&a())[2];

...declares a to be a zero-argument function which returns a reference to an integer array of size 2. (Read the basic rules on the link above to have a clear understanding of how I came up with that.)

int b()[2];

...declares b to be a zero-argument function which returns an integer array of size two.

Hope this helps.

Andrew Song
That website is very handy, thanks for that
GRB