static_assert.h 870 B

1234567891011121314151617181920212223
  1. //static_assert.h
  2. //portable solution compatible with C++98
  3. #if (__cplusplus < 201103L) //std < C++11
  4. //source http://www.pixelbeat.org/programming/gcc/STATIC_ASSERT.html
  5. #define ASSERT_CONCAT_(a, b) a##b
  6. #define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
  7. // These can't be used after statements in c89.
  8. #ifdef __COUNTER__
  9. #define static_assert(e,m) \
  10. ;enum { ASSERT_CONCAT(STATIC_ASSERT_, __COUNTER__) = 1/(int)(!!(e)) }
  11. #else
  12. //This can't be used twice on the same line so ensure if using in headers
  13. //that the headers are not included twice (by wrapping in #ifndef...#endif)
  14. //Note it doesn't cause an issue when used on same line of separate modules
  15. //compiled with gcc -combine -fwhole-program.
  16. #define static_assert(e,m) \
  17. ;enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(int)(!!(e)) }
  18. #endif //__COUNTER__
  19. #endif //(__cplusplus < 201103L)