我需要编写一个在bitarray上运行的宏,如下所示:
array[0] = number of bits in bitarray (integer)
array[1..n] = bits
Run Code Online (Sandbox Code Playgroud)
宏必须看起来像:
GetBit(pointer, index)
Macro *must* return 0,1 or call function similar to exit().
Run Code Online (Sandbox Code Playgroud)
这是我应该写的我(工作)内联函数版本的宏:
static inline unsigned long GetBit(BitArray_t array, unsigned long index)
{
if ((index) >= array[0])
exit(EXIT_FAILURE);
else
return (GetBit_wo_boundary_checks(array,index));
}
Run Code Online (Sandbox Code Playgroud)
这就是我所拥有的:
#define GetBit(array,index)\
(((index) < array[0] || exit(EXIT_FAILURE)) ?\
GetBit_wo_boundary_checks(array,index) : 0)
Run Code Online (Sandbox Code Playgroud)
我的问题是,这种具有做索引边界检查(ⅰ<P [0])和出口它试图与GetBit_wo_boundary_checks访问未定义的存储之前(P,I).
我认为我可以通过将退出置于短路评估条件来解决这个问题,但我得到:"无效使用void表达式".
当index高于array [0]中定义的最大值时,有没有办法使这个表达式宏透明地exit()?
一种选择是在您的exit()
通话中使用逗号运算符,例如:
(exit(EXIT_FAILURE), 0)
Run Code Online (Sandbox Code Playgroud)
这是一个小小的hack-ish,但是这会使表达式返回值0
并且应该满足编译器.