Does this line declare a function? C ++

I read the Lithuanian question about SFINAE here , and I was wondering what exactly his code declares. The following is an example (no templates):

int (&a())[2]; 

What exactly is this announcing? What is the role of &? To add to my confusion, if I declare instead

 int b()[2]; 

I get a message about the declaration of a function that returns an array, while the first line does not have such an error (so it would be considered that the first declaration was not ). However, if I try to assign

 a = a; 

I get an error: I am trying to assign the function a ... so now it is . What is this thing?

+4
source share
3 answers

These are amazing programs called cdecl and C ++ decl. They are very useful for defining complex declarations, especially for Byzantine forms that use C and C ++ 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 link, b does not.

+17
source

For future reference, you may find this link useful if you have a very complex C / C ++ declaration for decryption:

How to read C declarations

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

 int (&a())[2]; 

... declares a a function with a null argument that returns a reference to an integer array of size 2. (Read the basic link rules above to have a clear idea of ​​how I did this).

 int b()[2]; 

... declares b a function of zero argument, which returns an integer array of size two.

Hope this helps.

+8
source
 int (&a())[2]; 

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

  int b()[2]; 

This declares the character b , which is a function that takes no arguments and returns a two-element array of integers ... this is not possible by language design.

This is relatively simple: get the operator precedence diagram, run the symbol name ( a ) and start applying the operators as you like. Record after each operation.

+2
source

Source: https://habr.com/ru/post/1286541/


All Articles