Talk:cpp/language/declarations
From cppreference.com
[edit] "init-declarator-seq"
I suppose that the init-declarator-seq (see Declarators section) is actually a init-declarator-list. At least I did not found any occurrence of init-declarator-seq in the draft N4659 -- B166ER (talk) 04:24, 5 August 2018 (PDT)
[edit] Should `partial template specialization` be listed?
168.235.105.19 01:27, 28 September 2018 (PDT)
- the page is retelling dcl.dcl/1, where partial specialization does not appear because it's a kind of template-declaration.. but fair point, here, clicking on template declaration doesn't take you to partial specializations. adding. --Cubbi (talk) 02:51, 28 September 2018 (PDT)
[edit] the Spiral/Clockwise Rule
I really think this page should somehow contains the spiral/clockwise rule. It's used to identify the type of a variable. This is definitely a life-saver, especially to newbies. I found this page from this answer. --Brainor (talk) 23:45, 14 March 2022 (PDT)
- Spiral rule doesn't work in general, e.g. int data[y][x], nor do any of the other rules like reading backwards or whatever. The only generalisable trick to understand some crazy declaration I know is look at it as an expression instead, where the left-most type is the result type of the expression and all other types have values substituted for them.
- Example a) there is some declaration int data[y][x], the type of
data
is such that data[y][x] results in int, sodata
is a thing that needs indexing twice to get an int, i.e. an array of array of ints (or 2d array of ints) - Example b) there is some declaration void (*ptr)(int), the type of
ptr
is such that (*ptr)(0) is void, soptr
is something that needs dereferencing and then called with an int with void result, i.e. a pointer to a function taking an int and returning void - Example c) there is some declaration void (*signal(int, void (*)(int)))(int), the type of
signal
is such that (*signal(0, ptr))(0) results in void, whereptr
is a value of type void (*)(int), sosignal
requires being called with an int and a void (*)(int), then dereferenced, then called with an int with void result, i.e.signal
is a function taking an int and a void (*)(int) and returning a pointer to a function taking an int and returning void --Ybab321 (talk) 08:43, 15 March 2022 (PDT)
- a fair point is that both "spiral" and "inside-out" and "declaration follows use" rules are in use wide enough to be acknowledged, at least as notes. Or perhaps as the description of the Example where we walk through complicated declarations. Along with a link to cdecl.org - I'll put one together --Cubbi (talk) 10:44, 15 March 2022 (PDT)
- I think you are right. spiral rule doesn't apply to int data[x][y] indeed. And your trick is correctly built on the definition of declaration, but I prefer a foreward explanation than a backward one, for it's more simple to understand. And of course, your solution is absolutely correct to backward deciphering the type of inner expression.
- After dug into the specifier and declarator in declarations, in order to make spiral rule correct, I think this rule can be changed into "right-left rule", where starting from the variable, go right until seeing ')', then go left until '(', then right and so on. (I also found this on a Chinese blog just for reference.) e.g.
- a) void (*ptr)(int).
ptr
is go right, meet ')', go left a pointer see '(' go right of a function which returns void. - b) int (*(*func[7][8])(int*))[5].
func
is right an array of 7 elements, each element is an array of 8 elements, each element is left a pointer to right a function, which returns left a pointer right to an array of 5 int. - c) int * const *a(int,float).
a
is → a function which returns ← a pointer to a const pointer to int. --Brainor (talk) 23:05, 18 March 2022 (PDT)