From 4544f3def42ae9b8c528223d8762ae3634c7124a Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Tue, 25 Sep 2012 11:49:40 -0500 Subject: base: Check for static_assert support and provide fallback C++11 has support for static_asserts to provide compile-time assertion checking. This is very useful when testing, for example, structure sizes to make sure that the compiler got the right alignment or vector sizes. --- src/base/compiler.hh | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src') diff --git a/src/base/compiler.hh b/src/base/compiler.hh index a95cb791c..7b1c53d87 100644 --- a/src/base/compiler.hh +++ b/src/base/compiler.hh @@ -31,6 +31,8 @@ #ifndef __BASE_COMPILER_HH__ #define __BASE_COMPILER_HH__ +#include "config/have_static_assert.hh" + //http://msdn2.microsoft.com/en-us/library/ms937669.aspx //http://msdn2.microsoft.com/en-us/library/aa448724.aspx //http://docs.sun.com/source/819-3688/sun.specific.html#marker-998278 @@ -56,4 +58,32 @@ #error "Need to define compiler options in base/compiler.hh" #endif +/* + * Define a compatibility macro that emulates the behavior of + * static_assert using template magic if the compiler doesn't have + * native support. + */ +#if !HAVE_STATIC_ASSERT + +template +struct static_assert_failure; + +template<> +struct static_assert_failure {}; + +/* The following macro causes the compiler to evaluate the size of the + * static_assert_failure struct. The templates are designed so that + * only static_assert_failure evaluates to a proper size, while + * static_assert_failure generates a compile time error. + */ +#define static_assert(expr, msg) \ + namespace ns_static_assert { \ + enum { \ + static_assert_ ## __LINE__ = \ + sizeof(static_assert_failure) \ + }; \ + } + +#endif + #endif // __BASE_COMPILER_HH__ -- cgit v1.2.3