diff --git a/CMakeLists.txt b/CMakeLists.txt index 3357fef61..28778aacb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1343,6 +1343,15 @@ add_library(libfastfetch OBJECT ${LIBFASTFETCH_SRC} ) +include(CheckCSourceCompiles) +check_c_source_compiles("int main(void){int arr[1];return _Countof(arr);}" COMPILER_SUPPORTS_COUNT_OF) +if(COMPILER_SUPPORTS_COUNT_OF) + message(STATUS "_Countof is supported by the compiler") + target_compile_definitions(libfastfetch PUBLIC FF_SUPPORTS_COUNT_OF=1) +else() + message(STATUS "_Countof is NOT supported by the compiler") +endif() + if(yyjson_FOUND) target_compile_definitions(libfastfetch PUBLIC FF_USE_SYSTEM_YYJSON=1) target_link_libraries(libfastfetch PUBLIC yyjson::yyjson) diff --git a/src/common/arrayUtils.h b/src/common/arrayUtils.h index ad251ba1e..83a49d6aa 100644 --- a/src/common/arrayUtils.h +++ b/src/common/arrayUtils.h @@ -3,7 +3,9 @@ #include #ifdef __has_builtin - #if __has_builtin(__is_array) + #if !__cplusplus && FF_SUPPORTS_COUNT_OF + #define ARRAY_SIZE(x) _Countof(x) + #elif __has_builtin(__is_array) #define ARRAY_SIZE(x) ({ static_assert(__is_array(__typeof__(x)), "Must be an array"); (uint32_t) (sizeof(x) / sizeof(*(x))); }) #elif __has_builtin(__builtin_types_compatible_p) #define ARRAY_SIZE(x) ({ static_assert(!__builtin_types_compatible_p(__typeof__(x), __typeof__(&*(x))), "Must not be a pointer"); (uint32_t) (sizeof(x) / sizeof(*(x))); })