From 002e794ae2300d68fadac0b49fc9e15e896f8f5f Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 14 Jul 2019 18:28:51 -0400 Subject: Use C++ static_assert from N3928 when available --- misc.h | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'misc.h') diff --git a/misc.h b/misc.h index c405c4ab..23093be3 100644 --- a/misc.h +++ b/misc.h @@ -142,8 +142,13 @@ class Integer; #if CRYPTOPP_DOXYGEN_PROCESSING /// \brief Compile time assertion /// \param expr the expression to evaluate -/// \details Asserts the expression expr though a dummy struct. -#define CRYPTOPP_COMPILE_ASSERT(expr) { ... } +/// \details Asserts the expression expr during compile. If C++14 and +/// N3928 are available, then C++14 static_assert is used. Otherwise, +/// a CompileAssert structure is used. When the structure is used +/// a negative-sized array triggers the assert at compile time. +# define CRYPTOPP_COMPILE_ASSERT(expr) { ... } +#elif defined(CRYPTOPP_CXX11_STATIC_ASSERT) +# define CRYPTOPP_COMPILE_ASSERT(expr) static_assert(expr) #else // CRYPTOPP_DOXYGEN_PROCESSING template struct CompileAssert @@ -151,22 +156,24 @@ struct CompileAssert static char dummy[2*b-1]; }; -#define CRYPTOPP_COMPILE_ASSERT(assertion) CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, __LINE__) +#define CRYPTOPP_COMPILE_ASSERT(assertion) \ + CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, (__LINE__-1)) +#define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y) +#define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y + #if defined(CRYPTOPP_EXPORTS) || defined(CRYPTOPP_IMPORTS) -#define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) +# define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) #else -# if defined(__GNUC__) +# if defined(__GNUC__) || defined(__clang__) # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \ - static CompileAssert<(assertion)> \ - CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) __attribute__ ((unused)) + static CompileAssert<(assertion)> \ + CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) __attribute__ ((unused)) # else # define CRYPTOPP_COMPILE_ASSERT_INSTANCE(assertion, instance) \ - static CompileAssert<(assertion)> \ - CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) -# endif // __GNUC__ + static CompileAssert<(assertion)> \ + CRYPTOPP_ASSERT_JOIN(cryptopp_CRYPTOPP_ASSERT_, instance) +# endif // GCC or Clang #endif -#define CRYPTOPP_ASSERT_JOIN(X, Y) CRYPTOPP_DO_ASSERT_JOIN(X, Y) -#define CRYPTOPP_DO_ASSERT_JOIN(X, Y) X##Y #endif // CRYPTOPP_DOXYGEN_PROCESSING -- cgit v1.2.1