In a testdir for the 'assert-h' module, I'm seeing this compilation 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_WIN7 -I/usr/local/msvc64/include -MD -c -o test-assert-h-c++.obj `cygpath -w '../../gltests/test-assert-h-c++.cc'` test-assert-h-c++.cc C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xkeycheck.h(224): warning C4005: 'static_assert': macro redefinition ../gllib\assert.h(295): note: see previous definition of 'static_assert' C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xkeycheck.h(250): fatal error C1189: #error: The C++ Standard Library forbids macroizing keywords. Enable warning C4005 to find the forbidden macro. make[4]: *** [Makefile:746: test-assert-h-c++.obj] Error 2
If we don't define static_assert as a macro, it supports only the 2-arguments case, not the use with a single argument. And if we do define it as a macro, xkeycheck.h complains about it. Fortunately, there's a way to skip this check. 2023-02-05 Bruno Haible <br...@clisp.org> assert-h, verify: Fix compilation error in C++ mode with MSVC 14. * lib/verify.h (static_assert): Define _ALLOW_KEYWORD_MACROS. * tests/test-assert-h-c++.cc: Strengthen test. * tests/test-assert-h-c++2.cc: Likewise. diff --git a/lib/verify.h b/lib/verify.h index 8f786af7f5..f0b3fc5851 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -265,6 +265,8 @@ template <int w> # define _GL_SA3 static_assert # define _GL_SA_PICK(x1,x2,x3,x4,...) x4 # define static_assert(...) _GL_EXPAND(_GL_SA_PICK(__VA_ARGS__,_GL_SA3,_GL_SA2,_GL_SA1)) (__VA_ARGS__) +/* Avoid "fatal error C1189: #error: The C++ Standard Library forbids macroizing keywords." */ +# define _ALLOW_KEYWORD_MACROS 1 # else # define static_assert _Static_assert /* C11 requires this #define. */ # endif diff --git a/tests/test-assert-h-c++.cc b/tests/test-assert-h-c++.cc index 6b76565633..7da8b377fd 100644 --- a/tests/test-assert-h-c++.cc +++ b/tests/test-assert-h-c++.cc @@ -26,6 +26,9 @@ #include <iostream> +static_assert (2 + 2 == 4, "arithmetic does not work"); +static_assert (2 + 2 == 4); + int main () { diff --git a/tests/test-assert-h-c++2.cc b/tests/test-assert-h-c++2.cc index ffd7c8cf92..8808886202 100644 --- a/tests/test-assert-h-c++2.cc +++ b/tests/test-assert-h-c++2.cc @@ -22,3 +22,7 @@ /* Check against conflicts between <cassert> and other C++ header files. */ #include <stddef.h> #include <iostream> + + +static_assert (2 + 2 == 4, "arithmetic does not work"); +static_assert (2 + 2 == 4);