由于我工作的公司禁止使用boost,因此我需要在纯C ++中实现其功能。我已经研究了增强源,但它们似乎太复杂了,至少对我而言。我知道C ++ 0x standart中有一些名为static_assert()
的东西,但我不想使用任何C ++ 0x功能。
答案 0 :(得分:24)
另一个技巧(可以在C中使用)是在断言失败的情况下尝试构建负数大小的数组:
#define ASSERT(cond) int foo[(cond) ? 1 : -1]
作为奖励,您可以使用typedef而不是对象,以便它可以在更多上下文中使用,并且在成功时不会发生:
#define ASSERT(cond) typedef int foo[(cond) ? 1 : -1]
最后,建立一个名称冲突的可能性较小的名称(并且至少可以在不同的行中重用):
#define CAT_(a, b) a ## b
#define CAT(a, b) CAT_(a, b)
#define ASSERT(cond) typedef int CAT(AsSeRt, __LINE__)[(cond) ? 1 : -1]
答案 1 :(得分:21)
template<bool> struct StaticAssert;
template<> struct StaticAssert<true> {};
int main() {
StaticAssert< (4>3) >(); //OK
StaticAssert< (2+2==5) >(); //ERROR
}
答案 2 :(得分:18)
这是我自己的代码库提取的静态断言实现:Pre-C++11 Static Assertions Without Boost
。
用法:
STATIC_ASSERT(expression, message);
当静态断言测试失败时,会生成以某种方式包含STATIC_ASSERTION_FAILED_AT_LINE_xxx_message
的编译器错误消息。
message
必须是有效的C ++标识符,如no_you_cant_have_a_pony
,它将产生包含以下内容的编译器错误:
STATIC_ASSERTION_FAILED_AT_LINE_1337_no_you_cant_have_a_pony
:)
#define CONCATENATE(arg1, arg2) CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2) CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2) arg1##arg2
/**
* Usage:
*
* <code>STATIC_ASSERT(expression, message)</code>
*
* When the static assertion test fails, a compiler error message that somehow
* contains the "STATIC_ASSERTION_FAILED_AT_LINE_xxx_message" is generated.
*
* /!\ message has to be a valid C++ identifier, that is to say it must not
* contain space characters, cannot start with a digit, etc.
*
* STATIC_ASSERT(true, this_message_will_never_be_displayed);
*/
#define STATIC_ASSERT(expression, message)\
struct CONCATENATE(__static_assertion_at_line_, __LINE__)\
{\
implementation::StaticAssertion<static_cast<bool>((expression))> CONCATENATE(CONCATENATE(CONCATENATE(STATIC_ASSERTION_FAILED_AT_LINE_, __LINE__), _), message);\
};\
typedef implementation::StaticAssertionTest<sizeof(CONCATENATE(__static_assertion_at_line_, __LINE__))> CONCATENATE(__static_assertion_test_at_line_, __LINE__)
// note that we wrap the non existing type inside a struct to avoid warning
// messages about unused variables when static assertions are used at function
// scope
// the use of sizeof makes sure the assertion error is not ignored by SFINAE
namespace implementation {
template <bool>
struct StaticAssertion;
template <>
struct StaticAssertion<true>
{
}; // StaticAssertion<true>
template<int i>
struct StaticAssertionTest
{
}; // StaticAssertionTest<int>
} // namespace implementation
STATIC_ASSERT(true, ok);
STATIC_ASSERT(false, ko);
int main()
{
return 0;
}
答案 3 :(得分:4)
您只需将宏从Boost source file复制到您自己的代码即可。如果您不需要支持Boost支持的所有编译器,您可以为编译器选择正确的定义,并省略该文件中的其余#ifdef
。
答案 4 :(得分:4)
我相信这应该有效:
template<bool> struct CompileTimeAssert;
template<> struct CompileTimeAssert<true>{};
#define STATIC_ASSERT(e) (CompileTimeAssert <(e) != 0>())
答案 5 :(得分:2)
我正在使用以下头文件,其中包含其他人的代码......
#ifndef STATIC_ASSERT__H
#define STATIC_ASSERT__H
/* ripped from http://www.pixelbeat.org/programming/gcc/static_assert.html */
#define ASSERT_CONCAT_(a, b) a##b
#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
/* These can't be used after statements in c89. */
#ifdef __COUNTER__
/* microsoft */
#define STATIC_ASSERT(e) enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1/(!!(e)) }
#else
/* This can't be used twice on the same line so ensure if using in headers
* that the headers are not included twice (by wrapping in #ifndef...#endif)
* Note it doesn't cause an issue when used on same line of separate modules
* compiled with gcc -combine -fwhole-program. */
#define STATIC_ASSERT(e) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
#endif
/* http://msdn.microsoft.com/en-us/library/ms679289(VS.85).aspx */
#ifndef C_ASSERT
#define C_ASSERT(e) STATIC_ASSERT(e)
#endif
#endif