Thomas Weller
Thomas Weller

Reputation: 59303

What's the name of all the square brackets?

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")]]
  8. the separation of a pair into its parts like auto [x, y] = std::make_pair(1, 2);

IMHO, the array assignment and array access brackets are called subscript operator. What about all the others? Do they have good names?

Upvotes: 3

Views: 1123

Answers (2)

HolyBlackCat
HolyBlackCat

Reputation: 96286

(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.

(8) — auto [x, y] — The whole thing is a "structured binding (declaration)", or a "decomposition declaration" (I've only seen the latter in Clang error messages, not in the standard). The brackets themselves don't have a separate name here. The thing enclosed in brackets is called an identifier-list in the grammar.

Upvotes: 9

Thomas Weller
Thomas Weller

Reputation: 59303

  1. is a new declarator as per [expr.new] (N4713, chapter 8.5.2.4, paragraph 5) and new expression [new.delete.array] (N4713, chapter 21.6.2.2) (when used with the new keyword).
  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)
  8. is a structured binding declaration as per [dcl.struct.bind] (N4713, chapter 11.5)

Source: C++20 Draft N4713

Upvotes: 5

Related Questions