Skip to content

Serge3leo/countof_ns

Repository files navigation

CMake on multiple platforms

C2y countof() platform independent implementation with C23/C++14

The macro countof_ns(array) returns the number of elements in its operand. The number of elements is determined by the type of the operand. The result is an integer. For variable-length arrays (VLA), the operand is evaluated, otherwise the operand is not evaluated, and the result is an integer constant expression.

countof_ns() is a portable implementation of the macro countof() draft C2y by means of the C23/C++14 standards or using extensions of the C language standards of previous editions, which are supported by most compilers.

Incomplete list of supported compilers: Clang (clang), GNU (gcc), classic Intel (icc), IntelLLVM (icx), LCC (MCST Elbrus), MSVC (Visual Studio), NVHPC (NVIDIA HPC Compiler), Pelles C, PGI (The Portland Group(?)), SunPro (Oracle Developer Studio), XL (IBM® XL C/C++ for AIX®), XLClang (IBM Clang-based XL).

Table of Contents

Install

Implementing the macro countofi_ns() consists of a single, dependency-free file include/countof_ns/countof_ns.h. You can simply copy this file to the desired location on the header search paths.

Alternatively, you can use FetchContent in your cmake project:

include(FetchContent)
FetchContent_Declare(
    CountofNS
    GIT_REPOSITORY https://github.com/Serge3leo/countof_ns.git
    GIT_TAG  83e28beb0c5ddb80e20abb9331ae0caa4291dca5 # v0.7.0-pre-examples
)
FetchContent_MakeAvailable(CountofNS)

See the example: examples/cmake_fetch_content/CMakeLists.txt.

Usage

#include "countof_ns/countof_ns.h"

For a more detailed description of the usage, see: include/countof_ns/countof_ns.h.

Use with C/C++ extensions

The macro countof_ns() is applicable to any arrays, including both extended zero-length arrays and arrays containing extended zero-length objects (empty structures, joins with an array of indeterminate size or structures with a single array of indeterminate size). Unfortunately, in these cases, countof_ns() is different from countof():

int a07[0][7];
int a00[0][0];
int a70[7][0];
volatile size_t n7 = 7;
int v70[n7][0];

static_assert(0 == countof_ns(a07), "== сountof(a07)");
static_assert(0 == countof_ns(a00),
              "== сountof(a00), Successful resolution of uncertainty 0/0");
#if !__cplusplus
		// C version of the macro, if 0/0 uncertainty is detected
		// at compile time and it is impossible to resolution it,
		// generates an error
	(void)countof_ns(a70);  // Compilation error
#else
	static_assert(7 == countof_ns(a70), "== сountof(a70)");
#endif
	// For VLA, the 0/0 uncertainty cannot be detected at compile time
assert(0 == countof_ns(v70));  // The result differs from countof(v70)

For a discussion of the implementation, see the article: The long-awaited operator _Countof (in Russian).

Contributing

Issues or PRs are accepted and welcome.

Disclaimer

Sorry for my best English. Alas, this file is actually a yandex translation of README.ru.md with minimal editorial changes.

Links

License

BSD-2-Clause © 2025 Сергей Леонтьев (leo@sai.msu.ru).

About

C23/C++14 platform independent implementation of C2y countof()

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors