]> Savannah Git Hosting - gnulib.git/commitdiff
verify: Avoid syntax error due to static_assert with MSVC 14 in C++.
authorBruno Haible <bruno@clisp.org>
Sun, 18 Sep 2022 19:17:11 +0000 (21:17 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 18 Sep 2022 19:17:11 +0000 (21:17 +0200)
* lib/verify.h (static_assert): For MSVC in C++ mode, use a definition
that supports both the two-arguments and the one-argument syntax.

ChangeLog
lib/verify.h

index b9240f94c1b5dc8b917edc9907c2bcb886d8e238..432b125c852086e621ef436e3bb164ad8c5df44b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2022-09-14  Bruno Haible  <bruno@clisp.org>
+
+       verify: Avoid syntax error due to static_assert with MSVC 14 in C++.
+       * lib/verify.h (static_assert): For MSVC in C++ mode, use a definition
+       that supports both the two-arguments and the one-argument syntax.
+
 2022-09-14  Bruno Haible  <bruno@clisp.org>
 
        posix_spawn-* tests: Fix test failure on mingw when libtool is in use.
index dc99beed60b80f1b6c639ab17b4f3370dd5ae93e..3d2cb91d65a6eb802c9c2e3f300070f8d59eb866 100644 (file)
@@ -224,7 +224,22 @@ template <int w>
      _GL_VERIFY ((R), "static assertion failed", -)
 # endif
 # if __cpp_static_assert < 201411 && __GNUG__ < 6 && !defined static_assert
-#  define static_assert _Static_assert /* C11 requires this #define.  */
+#  if defined __cplusplus && _MSC_VER >= 1900 && !defined __clang__
+/* MSVC 14 in C++ mode supports the two-arguments static_assert but not
+   the one-argument static_assert, and it does not support _Static_assert.
+   We have to play preprocessor tricks to distinguish the two cases.
+   Since the MSVC preprocessor is not ISO C compliant (cf.
+   <https://stackoverflow.com/questions/5134523/>), the solution is specific
+   to MSVC.  */
+#   define _GL_EXPAND(x) x
+#   define _GL_SA1(a1) static_assert ((a1), "static assertion failed")
+#   define _GL_SA2 static_assert
+#   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__)
+#  else
+#   define static_assert _Static_assert /* C11 requires this #define. */
+#  endif
 # endif
 #endif