namespace shand { template <bool> struct static_assertion_faire; template <> struct static_assertion_faire<true> { enum { value = 1 }; }; #define SHAND_STATIC_ASSERT(Expression) shand::static_assertion_faire<Expression>::value } // namespace shand
#include <shand/static_assert.hpp> int main() { SHAND_STATIC_ASSERT(sizeof(int) == sizeof(double)); // エラー! return 0; }
※長めの引数?(テンプレート引数が2つ以上あるものとか)の場合はカッコで囲む必要がある
#include <shand/type_traits.hpp> #include <shand/static_assert.hpp> int main() { SHAND_STATIC_ASSERT(shand::is_same<int, int>::value); // エラー! SHAND_STATIC_ASSERT((shand::is_same<int, int>::value)); // OK return 0; }