With the last patch, a testdir for stdalign fails to compile on MSVC: source='../../gltests/test-stdalign-c++.cc' object='test-stdalign-c++.obj' libtool=no \ DEPDIR=.deps depmode=msvc7 /bin/sh ../../build-aux/depcomp \ /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-stdalign-c++.obj `cygpath -w '../../gltests/test-stdalign-c++.cc'` test-stdalign-c++.cc C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xkeycheck.h(170): warning C4005: 'alignas': macro redefinition ../gllib\stdalign.h(121): note: see previous definition of 'alignas' C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xkeycheck.h(171): warning C4005: 'alignof': macro redefinition ../gllib\stdalign.h(75): note: see previous definition of 'alignof' 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:1616: test-stdalign-c++.obj] Error 2
The problem is that the MSVC header files don't like overridden C++ keywords. Previously, this error was hidden because the first system include file — <stddef.h> in this case — was included before 'alignas' and 'alignof' were defined, and thus when xkeycheck.h was included by other system include files it had no effect. In fact there is no need to define 'alignas' and 'alignof' with this compiler, since it already has the keywords (in C++ mode): <https://learn.microsoft.com/en-us/cpp/cpp/alignment-cpp-declarations?view=msvc-170> <https://learn.microsoft.com/en-us/cpp/cpp/alignof-operator?view=msvc-170> This patch simplifies the 'alignas' and 'alignof' handling with this C++ compiler and moves the conflict check to the unit tests. With this, the compilation error goes away again. 2022-09-24 Bruno Haible <br...@clisp.org> stdalign: Fix compilation error with MSVC in C++ mode. * lib/stdalign.in.h: Treat MSVC in C++ mode like C++11 compliant compilers. * tests/test-stdalign-c++.cc: Include some other header files. diff --git a/lib/stdalign.in.h b/lib/stdalign.in.h index 9ba40e2c45..58fd245c62 100644 --- a/lib/stdalign.in.h +++ b/lib/stdalign.in.h @@ -58,7 +58,7 @@ && !defined __clang__) \ || (defined __clang__ && __clang_major__ < 8)) # ifdef __cplusplus -# if 201103 <= __cplusplus +# if (201103 <= __cplusplus || defined _MSC_VER) # define _Alignof(type) alignof (type) # else template <class __t> struct __alignof_helper { char __a; __t __b; }; @@ -70,7 +70,7 @@ # define _GL_STDALIGN_NEEDS_STDDEF 1 # endif #endif -#if ! (defined __cplusplus && 201103 <= __cplusplus) +#if ! (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER)) # define alignof _Alignof #endif #define __alignof_is_defined 1 @@ -101,7 +101,7 @@ */ #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 201112 -# if defined __cplusplus && 201103 <= __cplusplus +# if defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER) # define _Alignas(a) alignas (a) # elif (!defined __attribute__ \ && ((defined __APPLE__ && defined __MACH__ \ @@ -115,11 +115,13 @@ # define _Alignas(a) __declspec (align (a)) # endif #endif -#if ((defined _Alignas && ! (defined __cplusplus && 201103 <= __cplusplus)) \ +#if ((defined _Alignas \ + && !(defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) \ || (defined __STDC_VERSION__ && 201112 <= __STDC_VERSION__)) # define alignas _Alignas #endif -#if defined alignas || (defined __cplusplus && 201103 <= __cplusplus) +#if (defined alignas \ + || (defined __cplusplus && (201103 <= __cplusplus || defined _MSC_VER))) # define __alignas_is_defined 1 #endif diff --git a/tests/test-stdalign-c++.cc b/tests/test-stdalign-c++.cc index 8d5258e5f9..598b3c1da4 100644 --- a/tests/test-stdalign-c++.cc +++ b/tests/test-stdalign-c++.cc @@ -21,6 +21,10 @@ #include <stdalign.h> +/* Check against conflicts between <stdalign.h> and the C++ header files. */ +#include <stddef.h> +#include <iostream> + int main ()