Commit df1a699e5ba3232f373790b2c9485ddf720c4a70 introduced a
StaticAssertStmt() into a header file, which will fail if a module
written in C++ uses that header file.  Currently, that header file is
not widely used, but it's a potential problem if the use of static
assertions expands.

As discussed in
<https://www.postgresql.org/message-id/7775.1492448...@sss.pgh.pa.us>, a
more general solution would be to add specific C++ support for static
assertions in c.h.  Here is a patch for that, extracted from my
previously posted C++ patch set, but also a bit reworked from what was
previously posted.

Also attached is a little C++ test file that one can use to test this
out.  (Just compiling it should cause a compiler error without the patch
and a static assertion failure with the patch.)

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From 93adbf7bfa7afc1c8e25b037fce8227f3a225639 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pete...@gmx.net>
Date: Tue, 30 Aug 2016 12:00:00 -0400
Subject: [PATCH v3] Add support for static assertions in C++

This allows modules written in C++ to use or include header files that
use StaticAssertStmt() etc.
---
 src/include/c.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/include/c.h b/src/include/c.h
index af799dc1df..e238edb7d1 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -749,6 +749,7 @@ typedef NameData *Name;
  * about a negative width for a struct bit-field.  This will not include a
  * helpful error message, but it beats not getting an error at all.
  */
+#ifndef __cplusplus
 #ifdef HAVE__STATIC_ASSERT
 #define StaticAssertStmt(condition, errmessage) \
        do { _Static_assert(condition, errmessage); } while(0)
@@ -760,6 +761,18 @@ typedef NameData *Name;
 #define StaticAssertExpr(condition, errmessage) \
        StaticAssertStmt(condition, errmessage)
 #endif                                                 /* HAVE__STATIC_ASSERT 
*/
+#else                                                  /* C++ */
+#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
+#define StaticAssertStmt(condition, errmessage) \
+       static_assert(condition, errmessage)
+#else
+/* not worth providing a workaround */
+#define StaticAssertStmt(condition, errmessage) \
+       ((void) 0)
+#endif
+#define StaticAssertExpr(condition, errmessage) \
+       ({ StaticAssertStmt(condition, errmessage); true; })
+#endif                                                 /* C++ */
 
 
 /*

base-commit: b5c75feca7ffb2667c42b86286e262d6cb709b76
-- 
2.14.1

extern "C" {
#include "postgres.h"
}

extern "C"
int
somefunction(void)
{
        StaticAssertStmt(1 + 1 == 3, "arithmetic!");

        return 0;
}
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to