In C , we have square brackets in different places and I think it's sometimes important to distinguish them when talking to other developers. While I can call all of them "square brackets", I think they have better names, depending on what they do.
I am thinking of
- array declaration, like
int arr[1024];
- array assignment, like
arr[13] = 17;
- array access, like
int x = arr[13];
- map (and other container) access, like
int y = map["key"];
- captures in lambdas, like
auto lambda = [&](){return 23 arr[13];};
- the ones in
delete[]
- those of attributes like
[[deprecated("for reasons")]]
IMHO, the array assignment and array access brackets are called subscript operator. What about all the others? Do they have good names?
CodePudding user response:
- is a new expression as per [new.delete.array] (N4713, chapter 21.6.2.2)
- Array assignment seems to be called subscript operator as per C 20 draft [expr.sub] (N4713, chapter 8.5.1.1).
- same as 2.
- probably same as 2., I didn't find a better reference yet
- is a labmda introducer as per [expr.prim.lambda] (N4713, chapter 8.4.5)
delete
is the delete expression operator as per C 20 draft [expr.delete] (N4713, chapter 8.5.2.5). The array version of it is just an alternative.- is an attribute specifier as per [dcl.attr.grammar] (N4713, chapter 10.6.1)
Source: C 20 Draft N4713
CodePudding user response:
(2), (3), (4) — arr[13]
— It's an operator. So, "subscript operator" or "square brackets operator"? To further point out the lhs type, "{map,vector,array} subscript operator"?
(1) — int arr[1024];
— The grammar doesn't seem to have a name specifically for the brackets. The whole arr[1024]
is an "(array) declarator".
My colleague called the array declaration brackets (1.) "subscript operator" and I felt that this is the wrong term
I would point out that it's not an operator, without suggesting an other term. Just call them brackets.
(5) — [...](){}
— This is commonly called a "lambda capture list". The grammar calls it a "lambda-introducer", but the term feels rather obscure.
(6) — delete[]
— The whole thing is an array delete (expression). The brackets themselves don't have a separate name.
(7) — [[nodiscard]]
— The whole thing is an "attribute" (the grammar calls it an "attribute-specifier", or "...-seq" for a list of attributes). The double brackets themselves don't seem to have a separate name.