I am currently in the process of learning C++ from the book C++ Primer. In this book, it goes over complex C++ type declarations such as the following:
int *a[10];
int (&a)[10];
I understand how simple declarations with a single modifier such as the following work:
int v;
int *a = &v;
int &b = v;
The book only lists examples of the complex type but does not give any detailed rules for how they work. It also mentions the idea of reading in one direction or another in specific cases, but, this meaningless to me as the declaration int *a[10];
when read left to right could be "it is a pointer to an array" or it could be "it is a set of pointers in an array."
Also, the second declaration (int (&a)10
) is said to mean a reference to an array of 10 elements, but, since items in parenthesis are usually treated as a single item, it seems like it would make more sense to combine the &a
segment to mean "reference to int" and then add the [10]
section to turn it into an array.
I am looking for formal, well-defined, rules for understanding and writing these kinds of complex declarations. I am NOT looking for "strategies" or "tricks" to understand them -- I want the formal rules that someone looking to write a compiler would use.