From 67f5f506dd13bac5e54790e864511c9eb3f5d88b Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Sat, 17 Jan 2026 14:51:36 +0100
Subject: [PATCH v6 4/5] Support using StaticAssert macros in C++

StaticAssertExpr didn't work in MSVC C++. This adds a dedicated C++
definition which uses an inline lamdbda, that calls only the static
assert. Since this results in empty lambda, the actual call will be
removed by any compiler that does some optimization.
---
 src/include/c.h                                          | 8 ++++++--
 src/test/modules/test_cplusplusext/test_cplusplusext.cpp | 5 +++++
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/include/c.h b/src/include/c.h
index 65173b9d9fd..03764571dc8 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -956,13 +956,17 @@ pg_noreturn extern void ExceptionalCondition(const char *conditionName,
 	static_assert(condition, errmessage)
 #define StaticAssertStmt(condition, errmessage) \
 	do { static_assert(condition, errmessage); } while(0)
-#ifdef HAVE_STATEMENT_EXPRESSIONS
+#ifdef __cplusplus
+/* C++11 lambdas provide a convenient way to use static_assert as an expression */
+#define StaticAssertExpr(condition, errmessage) \
+	((void) ([](){ static_assert(condition, errmessage); }(), 0))
+#elif defined(HAVE_STATEMENT_EXPRESSIONS)
 #define StaticAssertExpr(condition, errmessage) \
 	((void) ({ static_assert(condition, errmessage); true; }))
 #else
 #define StaticAssertExpr(condition, errmessage) \
 	((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
-#endif							/* HAVE_STATEMENT_EXPRESSIONS */
+#endif
 
 
 /*
diff --git a/src/test/modules/test_cplusplusext/test_cplusplusext.cpp b/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
index 48741f27949..bb9c310504e 100644
--- a/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
+++ b/src/test/modules/test_cplusplusext/test_cplusplusext.cpp
@@ -24,6 +24,8 @@ PG_MODULE_MAGIC_EXT("test_cplusplusext", "1.2");
 PG_FUNCTION_INFO_V1(test_cplusplus_add);
 }
 
+StaticAssertDecl(sizeof(int32) == 4, "int32 should be 4 bytes");
+
 /*
  * Simple function that returns the sum of two integers.  This verifies that
  * C++ extension modules can be loaded and called correctly at runtime.
@@ -36,6 +38,9 @@ test_cplusplus_add(PG_FUNCTION_ARGS)
 	RangeTblRef *node = makeNode(RangeTblRef);
 	RangeTblRef *copy = copyObject(node);
 
+	StaticAssertStmt(sizeof(int32) == 4, "int32 should be 4 bytes");
+	(void) StaticAssertExpr(sizeof(int64) == 8, "int64 should be 8 bytes");
+
 	pfree(copy);
 	pfree(node);
 
-- 
2.52.0

