Add C11 static_assert macro to assert.h

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Add C11 static_assert macro to assert.h

Martin Wanvik
Taken from FreeBSD. I removed a comment to the effect that C's
_Static_assert, if implemented as a macro,
would not work well with C++ due to the possibility of unshielded
commas appearing in template parameter lists,
explaining why *all* C++ standards are excluded rather than just C++11
and later (where static_assert is a keyword).
I don't see why anyone would expect to combine C++03 or C++98 with C11
features, so I prefer to keep things
short (but I'll happily resubmit if anyone thinks otherwise).

Virtually identical to a patch submitted by Jonathan Gray in October
2016, which received no followup
(so I'll assume it wasn't rejected outright, but simply forgotten about).

Index: include/assert.h
RCS file: /cvs/src/include/assert.h,v
retrieving revision 1.13
diff -u -r1.13 assert.h
--- include/assert.h    26 Jun 2011 21:11:41 -0000    1.13
+++ include/assert.h    21 Mar 2020 14:01:13 -0000
@@ -61,6 +61,11 @@

 #ifndef _ASSERT_H_
 #define _ASSERT_H_
+#if __ISO_C_VISIBLE >= 2011 && !defined(__cplusplus)
+#define static_assert    _Static_assert
 __dead void __assert(const char *, int, const char *);
 __dead void __assert2(const char *, int, const char *, const char *);