Skip to content

Commit a2c8e5c

Browse files
committed
Add support for static assertions in C++
This allows modules written in C++ to use or include header files that use StaticAssertStmt() etc. Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
1 parent 0689dc3 commit a2c8e5c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/include/c.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ typedef NameData *Name;
754754
* about a negative width for a struct bit-field. This will not include a
755755
* helpful error message, but it beats not getting an error at all.
756756
*/
757+
#ifndef __cplusplus
757758
#ifdef HAVE__STATIC_ASSERT
758759
#define StaticAssertStmt(condition, errmessage) \
759760
do { _Static_assert(condition, errmessage); } while(0)
@@ -765,6 +766,19 @@ typedef NameData *Name;
765766
#define StaticAssertExpr(condition, errmessage) \
766767
StaticAssertStmt(condition, errmessage)
767768
#endif /* HAVE__STATIC_ASSERT */
769+
#else /* C++ */
770+
#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
771+
#define StaticAssertStmt(condition, errmessage) \
772+
static_assert(condition, errmessage)
773+
#define StaticAssertExpr(condition, errmessage) \
774+
StaticAssertStmt(condition, errmessage)
775+
#else
776+
#define StaticAssertStmt(condition, errmessage) \
777+
do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
778+
#define StaticAssertExpr(condition, errmessage) \
779+
({ StaticAssertStmt(condition, errmessage); })
780+
#endif
781+
#endif /* C++ */
768782

769783

770784
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy