[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PROPOSED 2/2] assert-h: prefer to ‘verify’
From: |
Bruno Haible |
Subject: |
Re: [PROPOSED 2/2] assert-h: prefer to ‘verify’ |
Date: |
Wed, 14 Sep 2022 13:35:47 +0200 |
Paul Eggert wrote:
> Where it’s easy, prefer ‘static_assert’ to ‘verify’,
On MSVC 14, in C mode, I get compilation errors:
...\gnulib-tests\test-assert.c(27): warning C4116: unnamed type definition in
parentheses
...\gnulib-tests\test-assert.c(27): error C2057: expected constant expression
...\gnulib-tests\test-assert.c(27): error C2149:
'_gl_static_assert_error_if_negative': named bit field cannot have zero width
...\gnulib-tests\test-assert.c(43): warning C4116: unnamed type definition in
parentheses
...\gnulib-tests\test-assert.c(43): error C2057: expected constant expression
...\gnulib-tests\test-assert.c(43): error C2149:
'_gl_static_assert_error_if_negative': named bit field cannot have zero width
...\gnulib-tests\test-assert.c(55): warning C4116: unnamed type definition in
parentheses
...\gnulib-tests\test-assert.c(55): error C2057: expected constant expression
...\gnulib-tests\test-assert.c(55): error C2149:
'_gl_static_assert_error_if_negative': named bit field cannot have zero width
...\gnulib-tests\test-assert.c(67): warning C4116: unnamed type definition in
parentheses
...\gnulib-tests\test-assert.c(67): error C2057: expected constant expression
...\gnulib-tests\test-assert.c(67): error C2149:
'_gl_static_assert_error_if_negative': named bit field cannot have zero width
make[3]: *** [Makefile:7324: test-assert.obj] Error 2
How come? These lines from the test program
static_assert (2 + 2 == 4, "arithmetic does not work");
static_assert (2 + 2 == 4);
static_assert (sizeof (char) == 1, "sizeof does not work");
static_assert (sizeof (char) == 1);
macroexpand to
extern int (*_gl_static_assert_function2 (void)) [(!!sizeof (struct {
unsigned int _gl_static_assert_error_if_negative: (2 + 2 == 4, "arithmetic does
not work") ? 1 : -1; }))];
extern int (*_gl_static_assert_function3 (void)) [(!!sizeof (struct {
unsigned int _gl_static_assert_error_if_negative: (2 + 2 == 4) ? 1 : -1; }))];
extern int (*_gl_static_assert_function4 (void)) [(!!sizeof (struct {
unsigned int _gl_static_assert_error_if_negative: (sizeof (char) == 1, "sizeof
does not work") ? 1 : -1; }))];
extern int (*_gl_static_assert_function5 (void)) [(!!sizeof (struct {
unsigned int _gl_static_assert_error_if_negative: (sizeof (char) == 1) ? 1 :
-1; }))];
which obviously is nonsense, as the value of a comma expression is the last
part, not the first part.
This patch fixes it.
2022-09-14 Bruno Haible <bruno@clisp.org>
verify: Avoid syntax error due to static_assert with MSVC 14.
* lib/verify.h (_Static_assert): Pass only the first argument to
_GL_VERIFY.
diff --git a/lib/verify.h b/lib/verify.h
index 154a5b76bf..a0d597f3d4 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -223,8 +223,8 @@ template <int w>
/* _GL_STATIC_ASSERT_H is defined if this code is copied into assert.h. */
#ifdef _GL_STATIC_ASSERT_H
# if !defined _GL_HAVE__STATIC_ASSERT1 && !defined _Static_assert
-# define _Static_assert(...) \
- _GL_VERIFY (__VA_ARGS__, "static assertion failed", -)
+# define _Static_assert(R, ...) \
+ _GL_VERIFY ((R), "static assertion failed", -)
# endif
# if (!defined static_assert \
&& __STDC_VERSION__ < 202311 \