Search code examples
c++function-declaration

Does this line declare a function? C++


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?


Solution

  • 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.