26

由于在我工作的公司中禁止使用 boost,因此我需要在纯 C++ 中实现其功能。我已经研究了提升源,但它们似乎太复杂而无法理解,至少对我来说是这样。我知道static_assert()C++0x 标准中有一些东西,但我不想使用任何 C++0x 功能。

4

6 回答 6

27

另一个技巧(可以在 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]
于 2009-12-30T12:52:48.313 回答
21
template<bool> struct StaticAssert;
template<> struct StaticAssert<true> {};

int main() {
   StaticAssert< (4>3) >(); //OK
   StaticAssert< (2+2==5) >(); //ERROR
}
于 2009-12-30T12:45:08.497 回答
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;
}
于 2009-12-30T13:17:01.877 回答
4

您可以简单地将Boost 源文件中的宏复制到您自己的代码中。如果您不需要支持 Boost 支持的所有编译器,您只需为您的编译器选择正确的定义并省略该#ifdef文件中的其余 s。

于 2009-12-30T12:51:50.010 回答
4

我相信这应该有效:

template<bool> struct CompileTimeAssert;   
template<> struct CompileTimeAssert<true>{};
#define STATIC_ASSERT(e) (CompileTimeAssert <(e) != 0>())
于 2010-10-30T02:55:45.873 回答
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
于 2009-12-30T13:13:41.790 回答