]> Savannah Git Hosting - gnulib.git/commitdiff
verify: Use C++11 static_assert when available.
authorBruno Haible <bruno@clisp.org>
Tue, 12 Jan 2021 20:11:13 +0000 (21:11 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 12 Jan 2021 20:12:15 +0000 (21:12 +0100)
Reported by Alexandre Duret-Lutz <adl@lrde.epita.fr> in
<https://lists.gnu.org/archive/html/bug-gnulib/2021-01/msg00177.html>.

* lib/verify.h (_GL_HAVE_STATIC_ASSERT_CXX11): New macro.
(_GL_HAVE_STATIC_ASSERT_CXX17): Renamed from _GL_HAVE_STATIC_ASSERT1.
(_GL_VERIFY): Use static_assert when available with C++11 syntax.

ChangeLog
lib/verify.h

index 0d0144242ca7f895efc2d9c7935e47a1961add17..4d313cf5763200107dd8651ae6611c35628f4ae7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-01-12  Bruno Haible  <bruno@clisp.org>
+
+       verify: Use C++11 static_assert when available.
+       Reported by Alexandre Duret-Lutz <adl@lrde.epita.fr> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2021-01/msg00177.html>.
+       * lib/verify.h (_GL_HAVE_STATIC_ASSERT_CXX11): New macro.
+       (_GL_HAVE_STATIC_ASSERT_CXX17): Renamed from _GL_HAVE_STATIC_ASSERT1.
+       (_GL_VERIFY): Use static_assert when available with C++11 syntax.
+
 2021-01-11  Paul Eggert  <eggert@cs.ucla.edu>
 
        tempname: consume less entropy
index 3cdcdca56715e5a144054f9717c383de18740fbc..a9e75890fc7f03e078487ed230ac672383f129f0 100644 (file)
    per C2X.  This is supported by GCC 9.1 and later, and by clang in
    C++1z mode.
 
-   Define _GL_HAVE_STATIC_ASSERT1 if static_assert (R) works as per
+   Define _GL_HAVE_STATIC_ASSERT_CXX11 if static_assert (R, DIAGNOSTIC)
+   works as per C++11.  This is supported by GCC 6.1 and later, and by
+   clang in C++11 mode.
+
+   Define _GL_HAVE_STATIC_ASSERT_CXX17 if static_assert (R) works as per
    C++17.  This is supported by GCC 9.1 and later, and by clang in
    C++1z mode.
 
 # if 4 <= __clang_major__ && 201411 <= __cpp_static_assert
 #  define _GL_HAVE__STATIC_ASSERT1 1
 # endif
+# if 201103L <= __cplusplus \
+     || 6 <= __GNUC__ \
+     || (4 <= __clang_major__ && 200410 <= __cpp_static_assert)
+#  define _GL_HAVE_STATIC_ASSERT_CXX11 1
+# endif
 # if 201703L <= __cplusplus \
      || 9 <= __GNUC__ \
      || (4 <= __clang_major__ && 201411 <= __cpp_static_assert)
-#  define _GL_HAVE_STATIC_ASSERT1 1
+#  define _GL_HAVE_STATIC_ASSERT_CXX17 1
 # endif
 #endif
 
@@ -225,7 +234,9 @@ template <int w>
    Unfortunately, unlike C11, this implementation must appear as an
    ordinary declaration, and cannot appear inside struct { ... }.  */
 
-#if defined _GL_HAVE__STATIC_ASSERT
+#if defined _GL_HAVE_STATIC_ASSERT_CXX11
+# define _GL_VERIFY(R, DIAGNOSTIC, ...) static_assert (R, DIAGNOSTIC)
+#elif defined _GL_HAVE__STATIC_ASSERT
 # define _GL_VERIFY(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC)
 #else
 # define _GL_VERIFY(R, DIAGNOSTIC, ...)                                \
@@ -239,7 +250,7 @@ template <int w>
 #  define _Static_assert(...) \
      _GL_VERIFY (__VA_ARGS__, "static assertion failed", -)
 # endif
-# if !defined _GL_HAVE_STATIC_ASSERT1 && !defined static_assert
+# if !defined _GL_HAVE_STATIC_ASSERT_CXX17 && !defined static_assert
 #  define static_assert _Static_assert /* C11 requires this #define.  */
 # endif
 #endif