This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] support: Define support_static_assert for use from C and C++


On 01/01/2018 12:40 AM, Paul Eggert wrote:
Florian Weimer wrote:
_Static_assert is reserved in C++ as well, so I don't want to touch it. The GCC headers might eventually use it in a way which is not strictly equivalent to static_assert.

If we don't care about the difference (what is it, by the way?), then

Later C++ versions support a one-argument form. The set of accepted expressions varies as well, but I think the C++ variant has a superset, so we should be okay.

something like this should work:

#ifndef __cplusplus
# define static_assert _Static_assert
#endif

since "static_assert" is not reserved in C.

I wish these committees would coordinate better....

At least we control the definition of static_assert in C mode. I'm still unhappy that we override headers from support/. In this case, it is probably harmless, but in general, I really dislike this approach, which is why I invented support_static_assert.

Thanks,
Florian
Subject: [PATCH] support: Define static_assert in <support/check.h>
To: libc-alpha@sourceware.org

This makes TEST_COMPARE usable from C++.

2018-01-01  Florian Weimer  <fweimer@redhat.com>

	* support/check.h (support_static_assert): Define.
	(TEST_COMPARE): Use it.

diff --git a/support/check.h b/support/check.h
index 55a6f09f42..6fc4ca207e 100644
--- a/support/check.h
+++ b/support/check.h
@@ -86,6 +86,12 @@ void support_test_verify_exit_impl (int status, const char *file, int line,
    does not support reporting failures from a DSO.  */
 void support_record_failure (void);
 
+/* Define static_assert in C mode, in the same way as <assert.h>.  */
+#ifndef __cplusplus
+# undef static_assert
+# define static_assert _Static_assert
+#endif
+
 /* Compare the two integers LEFT and RIGHT and report failure if they
    are different.  */
 #define TEST_COMPARE(left, right)                                       \
@@ -96,15 +102,15 @@ void support_record_failure (void);
     __left_type __left_value = (left);                                  \
     __right_type __right_value = (right);                               \
     /* Prevent use with floating-point and boolean types.  */           \
-    _Static_assert ((__left_type) 1.0 == (__left_type) 1.5,             \
-                    "left value has floating-point type");              \
-    _Static_assert ((__right_type) 1.0 == (__right_type) 1.5,           \
-                    "right value has floating-point type");             \
+    static_assert ((__left_type) 1.0 == (__left_type) 1.5,              \
+                   "left value has floating-point type");               \
+    static_assert ((__right_type) 1.0 == (__right_type) 1.5,            \
+                   "right value has floating-point type");              \
     /* Prevent accidental use with larger-than-long long types.  */     \
-    _Static_assert (sizeof (__left_value) <= sizeof (long long),        \
-                    "left value fits into long long");                  \
-    _Static_assert (sizeof (__right_value) <= sizeof (long long),       \
-                    "right value fits into long long");                 \
+    static_assert (sizeof (__left_value) <= sizeof (long long),         \
+                   "left value fits into long long");                   \
+    static_assert (sizeof (__right_value) <= sizeof (long long),        \
+                   "right value fits into long long");                  \
     /* Make sure that integer conversions does not alter the sign.   */ \
     enum                                                                \
     {                                                                   \
@@ -117,10 +123,10 @@ void support_record_failure (void);
                                             && (sizeof (__right_value)  \
                                                 < sizeof (__left_value))) \
     };                                                                  \
-    _Static_assert (__left_is_unsigned == __right_is_unsigned           \
-                    || __unsigned_left_converts_to_wider                \
-                    || __unsigned_right_converts_to_wider,              \
-                    "integer conversions may alter sign of operands");  \
+    static_assert (__left_is_unsigned == __right_is_unsigned            \
+                   || __unsigned_left_converts_to_wider                 \
+                   || __unsigned_right_converts_to_wider,               \
+                   "integer conversions may alter sign of operands");   \
     /* Compare the value.  */                                           \
     if (__left_value != __right_value)                                  \
       /* Pass the sign for printing the correct value.  */              \

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]