在“C接口和实现”4.3的assert.h中:
#undef assert
#ifdef NDEBUG
#define assert(e) ((void)0)
#else
#include "except.h"
extern void assert(int e);
#define assert(e) ((void)((e)||(RAISE(Assert_Failed), 0)))
#endif
extern void assert(int e);
的目的是什么因为在assert.c
中,它是由assert
宏实现的。 最佳答案
如果您仔细阅读本书的这一节,您将看到它提供了assert()
的标准接口,但没有提供标准兼容的实现。Assert
接口定义了标准指定的assert(e)
,只是断言失败引发异常Assert_Failed
,而不是中止执行,并且不提供断言e的文本。
…从问题中显示的assert.h
中提取…assert
模拟了标准的定义,因此两个assert.h
头可以互换使用,这就是Assert_Failed
出现在except.h
中的原因这个接口的实现很简单:
断言c
#include "assert.h"
const Except_T Assert_Failed = { "Assertion failed" };
void (assert)(int e) {
assert(e);
}
您需要阅读本章前面的部分,以了解异常处理机制和
Except_T
(和except.h
)。关于c - C接口(interface)和实现:为什么同时有函数断言和宏断言?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22651935/