template <bool> struct CompileAssert { }; #undef COMPILE_ASSERT #define COMPILE_ASSERT(expr, msg) / typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] 在编译期就可以出现错误,但expr应该必须是个常量,这个还没试验过,这个其实还有另一个写法,如下 #undef COMPILE_ASSERT #define COMPILE_ASSERT(expr, msg) / typedef int msg[bool(expr) ? 1 : -1] 不知道为什么不这样写,实验过也是可以用的