[glib] Add G_STATIC_ASSERT_EXPR macro



commit c00ef0a17aba057698eab4d4a769b358511370cd
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun May 22 12:44:12 2011 -0400

    Add G_STATIC_ASSERT_EXPR macro
    
    https://bugzilla.gnome.org/show_bug.cgi?id=626549

 docs/reference/glib/tmpl/macros_misc.sgml |   18 ++++++++++++++++++
 glib/gmacros.h                            |    1 +
 2 files changed, 19 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/glib/tmpl/macros_misc.sgml b/docs/reference/glib/tmpl/macros_misc.sgml
index eb875bb..d0ef184 100644
--- a/docs/reference/glib/tmpl/macros_misc.sgml
+++ b/docs/reference/glib/tmpl/macros_misc.sgml
@@ -165,6 +165,24 @@ The macro should only be used once per source code line.
 @expr: a constant expression.
 @Since: 2.20
 
+<!-- ##### MACRO G_STATIC_ASSERT_EXPR ##### -->
+<para>
+The G_STATIC_ASSERT_EXPR macro lets the programmer check a condition at
+compile time.  The condition needs to be compile time computable.
+</para>
+<para>
+Unlike <literal>G_STATIC_ASSERT</literal>, this macro evaluates to an
+expression and, as such, can be used in the middle of other expressions.
+Its value should be ignored.  This can be accomplished by placing it as
+the first argument of a comma expression.
+</para>
+<informalexample><programlisting>
+#define ADD_ONE_TO_INT(x) \
+  (G_STATIC_ASSERT_EXPR(sizeof (x) == sizeof (int)), ((x) + 1))
+</programlisting></informalexample>
+
+ expr: a constant expression.
+ Since: 2.30
 
 <!-- ##### MACRO G_GNUC_EXTENSION ##### -->
 <para>
diff --git a/glib/gmacros.h b/glib/gmacros.h
index 9f9c25d..7c6fb00 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -147,6 +147,7 @@
 #define G_PASTE_ARGS(identifier1,identifier2) identifier1 ## identifier2
 #define G_PASTE(identifier1,identifier2)      G_PASTE_ARGS (identifier1, identifier2)
 #define G_STATIC_ASSERT(expr) typedef struct { char Compile_Time_Assertion[(expr) ? 1 : -1]; } G_PASTE (_GStaticAssert_, __LINE__)
+#define G_STATIC_ASSERT_EXPR(expr) ((void) sizeof (char[(expr) ? 1 : -1]))
 
 /* Provide a string identifying the current code position */
 #if defined(__GNUC__) && (__GNUC__ < 3) && !defined(__cplusplus)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]