Building a testdir of 'stdcountof-h' (with C++ tests) on Windows with MSVC 14,
I see this error:
/home/bruno/msvc/compile cl -nologo -DHAVE_CONFIG_H -I. -I../../gltests -I..
-DGNULIB_STRICT_CHECKING=1 -DIN_GNULIB_TESTS=1 -I. -I../../gltests -I..
-I../../gltests/.. -I../gllib -I../../gltests/../gllib
-D_WIN32_WINNT=_WIN32_WINNT_WINXP -I/usr/local/msvc32/include -MD -c -o
test-stdcountof-h-c++.obj `cygpath -w '../../gltests/test-stdcountof-h-c++.cc'`
test-stdcountof-h-c++.cc
c:\cygwin64\home\bruno\testdir2\gltests\test-stdcountof-h.c(81): error C4576: a
parenthesized type followed by an initializer list is a non-standard explicit
type conversion syntax
c:\cygwin64\home\bruno\testdir2\gltests\test-stdcountof-h.c(82): error C4576: a
parenthesized type followed by an initializer list is a non-standard explicit
type conversion syntax
make[4]: *** [Makefile:743: test-stdcountof-h-c++.obj] Fehler 2
I can't get this working by tweaking the generated stdcountof.h. The problem
is apparently the expression
array_expression[0]
where the array_expression is non-standard C++.
This patch works around the build failure.
2026-01-14 Bruno Haible <[email protected]>
stdcountof-h tests: Avoid compilation error in C++ mode.
* tests/test-stdcountof-h.c (test_func): Don't test ISO C initializer
syntax in C++ mode.
diff --git a/tests/test-stdcountof-h.c b/tests/test-stdcountof-h.c
index 191ba5c779..03498ae99d 100644
--- a/tests/test-stdcountof-h.c
+++ b/tests/test-stdcountof-h.c
@@ -78,8 +78,12 @@ test_func (int parameter[3])
ASSERT (countof (unbounded) >= 0);
#endif
+ /* Avoid MSVC C++ error C4576 "a parenthesized type followed by an
+ initializer list is a non-standard explicit type conversion syntax". */
+#if !defined __cplusplus
ASSERT (countof ((int[]) { integer, integer, integer }) == 3);
ASSERT (countof (((int[]) { integer, integer, integer })) == 3);
+#endif
/* Check that countof(...) is an expression of type size_t. */
#if !defined __cplusplus && HAVE__GENERIC