在extern C中使用_attribute__ ((nothrow))有意义吗?

Does it make sense to use _attribute__ ((nothrow)) inside extern C?

本文关键字:nothrow 有意义 attribute extern      更新时间:2023-10-16

我从c++中调用了一些C代码。
标题类似于以下内容:

#ifndef CLibH
#define CLibH
#ifdef __cplusplus
extern "C" {
#endif
//C API
void foo(void);
// ...
#ifdef __cplusplus
}
#endif
#endif

由于我已经使用extern C
添加nothrow编译器属性有什么好处吗?

#ifndef CLibH
#define CLibH
#ifdef __cplusplus
extern "C" {
#endif
//C API
void foo(void) __attribute__((nothrow));
// ...
#ifdef __cplusplus
}
#endif
#endif

extern C是否使此冗余?
在这种情况下应用它还有什么好处吗?

是的。来自gcc文档:

例如,标准C库中的大多数函数可以的异常保证不会抛出异常接受函数指针实参的Qsort和bsearch。