BOOST_STATIC_ASSERT without boost(BOOST_STATIC_ASSERT 没有提升)
问题描述
由于我工作的公司禁止使用 boost,因此我需要用纯 C++ 实现其功能.我已经研究了 boost 源,但它们似乎太复杂而无法理解,至少对我来说是这样.我知道 C++0x 标准中有一种叫做 static_assert() 的东西,但我不想使用任何 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]
作为奖励,您可以使用 typedef 而不是对象,这样它就可以在更多上下文中使用并且在成功时不会发生:
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 没有提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:BOOST_STATIC_ASSERT 没有提升
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
