[ovs-dev] [PATCH] lib/unaligned.h - Fix C++ compiler issue with BUILD_ASSERT on Windows

Sairam Venugopal vsairam at vmware.com
Tue Dec 19 23:32:27 UTC 2017


A static assert declaration may appear at block scope (as a block
declaration) and inside a class body (as a member declaration). However,
unlike sizeof, static_assert cannot be used as an expression and this is
strictly enforced on MSVC. error C2059: syntax error : static_assert

Signed-off-by: Sairam Venugopal <vsairam at vmware.com>
---
 lib/unaligned.h | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/unaligned.h b/lib/unaligned.h
index f40e4e1..6bc9063 100644
--- a/lib/unaligned.h
+++ b/lib/unaligned.h
@@ -51,12 +51,27 @@ static inline void put_unaligned_be64(ovs_be64 *, ovs_be64);
  *
  * Below, "sizeof (*(P) % 1)" verifies that *P has an integer type, since
  * operands to % must be integers.
+ *
+ * On MSVC C++ compiler throws error C2059: syntax error : ‘static_assert’.
+ * A static assert declaration may appear at block scope (as a block declaration)
+ * and inside a class body (as a member declaration). However, unlike sizeof,
+ * static_assert cannot be used as an expression and this is strictly enforced
+ * on MSVC. The workaround would be to use a lambda expression for C++.
+ *
  */
+#if defined(__cplusplus) && defined(WIN32)
 #define get_unaligned_u64(P)                                \
-    (BUILD_ASSERT(sizeof *(P) == 8),                        \
+    ([P]{BUILD_ASSERT(sizeof *(P) == 8);},                  \
      BUILD_ASSERT_GCCONLY(!TYPE_IS_SIGNED(typeof(*(P)))),   \
      (void) sizeof (*(P) % 1),                              \
      get_unaligned_u64__((const uint64_t *) (P)))
+#else
+ #define get_unaligned_u64(P)                                \
+     (BUILD_ASSERT(sizeof *(P) == 8),                        \
+      BUILD_ASSERT_GCCONLY(!TYPE_IS_SIGNED(typeof(*(P)))),   \
+      (void) sizeof (*(P) % 1),                              \
+      get_unaligned_u64__((const uint64_t *) (P)))
+#endif
 
 #ifdef __GNUC__
 /* GCC implementations. */
-- 
2.9.0.windows.1



More information about the dev mailing list