BOOST_STATIC_ASSERT没有提振 [英] BOOST_STATIC_ASSERT without boost
问题描述
由于升压是在一家公司工作,我为我需要在纯C来实现它的功能++禁止的。我看着提振来源,但他们似乎过于复杂,难于理解,至少对我来说。我知道有个东西叫 static_assert()
在C ++ 0x中旗,但我想不使用任何的C ++ 0x功能。
Since boost is forbidden in a company I work for I need to implement its functionality in pure C++. I've looked into boost sources but they seem to be too complex to understand, at least for me. I know there is something called static_assert()
in the C++0x standart, but I'd like not to use any C++0x features.
推荐答案
另外一个绝招(可以在C中使用的)是尝试,如果断言失败,消极的大小建立一个数组:
One other trick (which can be used in C) is to try to build an array with a negative size if the assert fail:
#define ASSERT(cond) int foo[(cond) ? 1 : -1]
作为奖励,你可以使用,而不是对象的类型定义,使其更加实用的上下文并没有发生,当它成功的:
as a bonus, you may use a typedef instead of an object, so that it is usable in more contexts and doesn't takes place when it succeed:
#define ASSERT(cond) typedef int foo[(cond) ? 1 : -1]
最后,建立一个名名称冲突的可能性就越小(和可重复使用,至少在不同线路):
finally, build a name with less chance of name clash (and reusable at least in different lines):
#define CAT_(a, b) a ## b
#define CAT(a, b) CAT_(a, b)
#define ASSERT(cond) typedef int CAT(AsSeRt, __LINE__)[(cond) ? 1 : -1]
这篇关于BOOST_STATIC_ASSERT没有提振的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!