Introduce and use the cool C2y countof operator
#14413
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Last year I rewrote the C code to use declarations in for loops init-clauses in #13289 (a C99 feature!). This year, I propose a fast-forward in time to introduce a new C2y operator,
_Countof. This operator counts the number of elements in an array. If the operand is not an array, it produces a compilation error. This adds a bit of type safety and consistency._Lengthof()operator (v4) (later renamed to_Countof);_Countof.See also, for
__has_featureand__has_extension:__has_featureand__has_extension;__has_feature;__has_extension.The trend since C11 seems to introduce new operators as
_Operator(as the_[A-Z]prefix is reserved by the standard for identifiers), introduce macros as#define operator _Operatorin a new<std….h>header, and in a later revision turn the macro into a regular keyword and make the header a no-op. If we were to include the lowercase identifier in a public header, it could interact badly with other projects also defining it. Regarding the spelling, my preference goes to usingcountofover_CountoforCAMLcountof. I've tried to come up with a backward and forward compatible definition ofcountof, that I've kept private underCAML_INTERNALS.This patch was generated using Coccinelle, with the semantic patch below. Only the
#define CAML_INTERNALSwere manually added, as needed.