- why?
Because Dennis Ritchie designed it to be so in C, and because C++ was designed to be compatible with C.
There weren't references in C of course, but references follow the same syntax rules as pointers do, which were inherited from C.
The main confusion however doesn't seem to be about where the reference puncutator goes, but rather about the array syntax. The array size simply always goes to the right side of the name. This same pattern is also true for variables:
int arr[size];
| | |
| | size
| name
element type
int (&arr_ref)[size];
| | |
| | size
| name
element type
int (&f())[10]
| | |
| | size
| name
element type
Function declaration is different only in the regard that it has the parameter list. Which is always immediately after the name. And because it is immediately after the name, it is consequently before the array size.
- Can it be written as below:
No. That is ill-formed in C++.
- I couldn't understand typedef usage when using it like:
typedef is simply an alias; a different name for the type.
how will the this code be simplified?
The code in 3. is optimally simple. Personally, I prefer the using syntax for type aliases instead of typedef:
using array_t = int[10];
Another option is to use trailing return type, although it whether this is more or less simple is subjective:
auto f() -> int(&)[10] {
and will it look like the code in point 1?
The decaration looks different, but it declares the same function.
why?? Because the language (standard) specifies it to be that way. What other reason would you expect? 2) No. 3)how will the this code be simplified?Please correct the question, "how will the".. what be simplified?will it look like the code in point 1?Well, obviously the snippets in point 1 and 3 "look" different. And yes, the function declarations in point 1 and 3 are compatible with each other.using array_t = int[10];