On 1/13/21 5:21 PM, Bruno Haible wrote:
For example, say, someone use g++ version >= 5 with option -std=c++98. Then, with your new code, __cpp_static_assert will be undefined, _GL_HAVE__STATIC_ASSERT will be 1, and _GL_VERIFY(R, DIAGNOSTIC, ...) will expand to _Static_assert (R, DIAGNOSTIC) which most likely leads to a syntax error.
Thanks, I see your point. (I had to use -std=gnu++98 to observe the problem.)
How about the attached, more-conservative patch instead? It limits __cpp_static_assert to controlling the use of 'static_assert', and causes C++ code to not use _Static_assert at all.
Also note that the C++ test coverage of this module is currently zero (none). This means that if you make this change and it is buggy, we have no way to catch it, before some user will stumble across it.
The users in this case are developers, and the failure mode is merely a trivial failure to compile, so these problems should be relatively minor. And when things go wrong (as they likely will regardless of whether this patch is installed :-), having a simpler verify.h will ease users' jobs.
From bedf196eeebe249f75f71a51a55edca5da78e027 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Wed, 13 Jan 2021 15:46:33 -0800 Subject: [PATCH] verify: simplify static_assert configuration * lib/verify.h (_GL_HAVE__STATIC_ASSERT, _GL_HAVE__STATIC_ASSERT1): Do not define for C++. This should be good enough nowadays, since recent-enough C++ compilers have static_assert. (_GL_HAVE_STATIC_ASSERT_CXX11, _GL_HAVE_STATIC_ASSERT_CXX17): Remove. All uses replaced by simply checking __cpp_static_assert. --- ChangeLog | 9 +++++++++ lib/verify.h | 35 ++++------------------------------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2123974c8..8991d6a35 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2021-01-13 Paul Eggert <egg...@cs.ucla.edu> + + verify: simplify static_assert configuration + * lib/verify.h (_GL_HAVE__STATIC_ASSERT, _GL_HAVE__STATIC_ASSERT1): + Do not define for C++. This should be good enough nowadays, + since recent-enough C++ compilers have static_assert. + (_GL_HAVE_STATIC_ASSERT_CXX11, _GL_HAVE_STATIC_ASSERT_CXX17): + Remove. All uses replaced by simply checking __cpp_static_assert. + 2021-01-13 Simon Josefsson <si...@josefsson.org> lib-msvc-compat: Update libtool usage recommendation. diff --git a/lib/verify.h b/lib/verify.h index a9e75890f..65514c34b 100644 --- a/lib/verify.h +++ b/lib/verify.h @@ -22,20 +22,10 @@ /* Define _GL_HAVE__STATIC_ASSERT to 1 if _Static_assert (R, DIAGNOSTIC) - works as per C11. This is supported by GCC 4.6.0 and later, in C - mode, and by clang (also in C++ mode). + works as per C11. This is supported by GCC 4.6.0+ and by clang 4+. Define _GL_HAVE__STATIC_ASSERT1 to 1 if _Static_assert (R) works as - per C2X. This is supported by GCC 9.1 and later, and by clang in - C++1z mode. - - 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. + per C2X. This is supported by GCC 9.1+. Support compilers claiming conformance to the relevant standard, and also support GCC when not pedantic. If we were willing to slow @@ -51,23 +41,6 @@ || (!defined __STRICT_ANSI__ && 9 <= __GNUC__)) # define _GL_HAVE__STATIC_ASSERT1 1 # endif -#else -# if 4 <= __clang_major__ -# define _GL_HAVE__STATIC_ASSERT 1 -# endif -# 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_ASSERT_CXX17 1 -# endif #endif /* FreeBSD 9.1 <sys/cdefs.h>, included by <stddef.h> and lots of other @@ -234,7 +207,7 @@ 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_CXX11 +#if 200410 <= __cpp_static_assert # define _GL_VERIFY(R, DIAGNOSTIC, ...) static_assert (R, DIAGNOSTIC) #elif defined _GL_HAVE__STATIC_ASSERT # define _GL_VERIFY(R, DIAGNOSTIC, ...) _Static_assert (R, DIAGNOSTIC) @@ -250,7 +223,7 @@ template <int w> # define _Static_assert(...) \ _GL_VERIFY (__VA_ARGS__, "static assertion failed", -) # endif -# if !defined _GL_HAVE_STATIC_ASSERT_CXX17 && !defined static_assert +# if __cpp_static_assert < 201411 && !defined static_assert # define static_assert _Static_assert /* C11 requires this #define. */ # endif #endif -- 2.27.0