diff mbox

[2/2] Use static_assert for STATIC_ASSERT for C++11 onwards

Message ID 1469575179-53321-2-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm July 26, 2016, 11:19 p.m. UTC
C++11 has a
  static_assert (COND, MESSAGE)
which gives more readable error messages for STATIC_ASSERT than our
current implementation.

This patch makes us use it if __cplusplus >= 201103L

There's also a provisional static_assert (COND) in C++1z, but presumably
we should wait until that one is fully standardized before using it.

Bootstrapped&regrtested on x86_64-pc-linux-gnu (in conjunction
with the previous patch)

OK for trunk?

gcc/ChangeLog:
	* system.h (STATIC_ASSERT): Use static_assert if building
	with C++11 onwards.
---
 gcc/system.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Comments

Richard Biener July 27, 2016, 8:09 a.m. UTC | #1
On Wed, Jul 27, 2016 at 1:19 AM, David Malcolm <dmalcolm@redhat.com> wrote:
> C++11 has a
>   static_assert (COND, MESSAGE)
> which gives more readable error messages for STATIC_ASSERT than our
> current implementation.
>
> This patch makes us use it if __cplusplus >= 201103L
>
> There's also a provisional static_assert (COND) in C++1z, but presumably
> we should wait until that one is fully standardized before using it.
>
> Bootstrapped&regrtested on x86_64-pc-linux-gnu (in conjunction
> with the previous patch)
>
> OK for trunk?

Ok.

Richard.

> gcc/ChangeLog:
>         * system.h (STATIC_ASSERT): Use static_assert if building
>         with C++11 onwards.
> ---
>  gcc/system.h | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/system.h b/gcc/system.h
> index 78a7da6..8a17197 100644
> --- a/gcc/system.h
> +++ b/gcc/system.h
> @@ -752,9 +752,14 @@ extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
>  #define STATIC_CONSTANT_P(X) (false && (X))
>  #endif
>
> -/* Until we can use C++11's static_assert.  */
> +/* static_assert (COND, MESSAGE) is available in C++11 onwards.  */
> +#if __cplusplus >= 201103L
> +#define STATIC_ASSERT(X) \
> +  static_assert ((X), #X)
> +#else
>  #define STATIC_ASSERT(X) \
>    typedef int assertion1[(X) ? 1 : -1] ATTRIBUTE_UNUSED
> +#endif
>
>  /* Provide a fake boolean type.  We make no attempt to use the
>     C99 _Bool, as it may not be available in the bootstrap compiler,
> --
> 1.8.5.3
>
diff mbox

Patch

diff --git a/gcc/system.h b/gcc/system.h
index 78a7da6..8a17197 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -752,9 +752,14 @@  extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
 #define STATIC_CONSTANT_P(X) (false && (X))
 #endif
 
-/* Until we can use C++11's static_assert.  */
+/* static_assert (COND, MESSAGE) is available in C++11 onwards.  */
+#if __cplusplus >= 201103L
+#define STATIC_ASSERT(X) \
+  static_assert ((X), #X)
+#else
 #define STATIC_ASSERT(X) \
   typedef int assertion1[(X) ? 1 : -1] ATTRIBUTE_UNUSED
+#endif
 
 /* Provide a fake boolean type.  We make no attempt to use the
    C99 _Bool, as it may not be available in the bootstrap compiler,