Home > Net >  What's the name of all the square brackets?
What's the name of all the square brackets?

Time:09-13

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

  1. array declaration, like int arr[1024];
  2. array assignment, like arr[13] = 17;
  3. array access, like int x = arr[13];
  4. map (and other container) access, like int y = map["key"];
  5. captures in lambdas, like auto lambda = [&](){return 23 arr[13];};
  6. the ones in delete[]
  7. 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:

  1. is a new expression as per [new.delete.array] (N4713, chapter 21.6.2.2)
  2. Array assignment seems to be called subscript operator as per C 20 draft [expr.sub] (N4713, chapter 8.5.1.1).
  3. same as 2.
  4. probably same as 2., I didn't find a better reference yet
  5. is a labmda introducer as per [expr.prim.lambda] (N4713, chapter 8.4.5)
  6. 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.
  7. 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.

  • Related