Reputation: 1555
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?
Upvotes: 4
Views: 714
Reputation: 3128
For future reference, you may find this link helpful when you have a particularly difficult C/C++ declaration to decipher:
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.
Upvotes: 8
Reputation: 41387
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.
Upvotes: 2
Reputation: 76660
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.
Upvotes: 17