关于c ++:为什么在C库中看到THROW?

Why do I see THROW in a C library?

当我做:
less /usr/include/stdio.h(这只是一个C库-与C ++无关)

在很多函数声明之后,我看到了__THROW
另外,在一些功能上的注释说:"此功能是可能的取消点,因此未标记为__THROW"。
这都是为了什么?

throw是用于异常处理的...但是据我所知,C对此不提供任何支持。

请解释。


该标头很可能在该供应商的C和C ++编译器之间共享。您看到了__THROW的定义吗?

我怀疑类似的东西:

1
2
3
4
5
#ifdef __cplusplus
    #define __THROW throw()
#else
    #define __THROW
#endif

或针对实际规格:

1
2
3
4
5
#ifdef __cplusplus
    #define __THROW(x) throw(x)
#else
    #define __THROW(x)
#endif

如您所见,在C构建中,它没有扩展。在C ++中,它可以满足您的期望。这使供应商可以重复使用同一文件。

仅对nitpick来说,这并不完全正确:"((这只是一个C库,与C ++无关)"

C ++标准库包括使用C标准库的功能。实际的标头是,其中xxx是C标头名称。也就是说,要在C ++中包含C头文件,请执行。因此,它确实与C ++有关。 :)

这就是为什么您看到自己的代码的原因。将标题复制为两种不同的语言将是维护和清洁的噩梦。


你可以做

1
cpp -include stdlib.h -dM /dev/null  |grep '#define __THROW '

了解它实际上扩展到了什么。

在我的系统上,我得到:

1
#define __THROW __attribute__ ((__nothrow__ __LEAF))

nothrow和leaf属性在以下位置描述
https://gcc.gnu.org/onlinedocs/gcc-6.1.0/gcc/Common-Function-Attributes.html#Common-Function-Attributes
如下:

leaf Calls to external functions with this attribute must return to
the current compilation unit only by return or by exception handling.
In particular, a leaf function is not allowed to invoke callback
functions passed to it from the current compilation unit, directly
call functions exported by the unit, or longjmp into the unit. Leaf
functions might still call functions from other compilation units and
thus they are not necessarily leaf in the sense that they contain no
function calls at all. The attribute is intended for library functions
to improve dataflow analysis. The compiler takes the hint that any
data not escaping the current compilation unit cannot be used or
modified by the leaf function. For example, the sin function is a leaf
function, but qsort is not.

Note that leaf functions might indirectly run a signal handler defined
in the current compilation unit that uses static variables. Similarly,
when lazy symbol resolution is in effect, leaf functions might invoke
indirect functions whose resolver function or implementation function
is defined in the current compilation unit and uses static variables.
There is no standard-compliant way to write such a signal handler,
resolver function, or implementation function, and the best that you
can do is to remove the leaf attribute or mark all such static
variables volatile. Lastly, for ELF-based systems that support symbol
interposition, care should be taken that functions defined in the
current compilation unit do not unexpectedly interpose other symbols
based on the defined standards mode and defined feature test macros;
otherwise an inadvertent callback would be added.

The attribute has no effect on functions defined within the current
compilation unit. This is to allow easy merging of multiple
compilation units into one, for example, by using the link-time
optimization. For this reason the attribute is not allowed on types to
annotate indirect calls.

h

nothrow The nothrow attribute is used to inform the compiler that a
function cannot throw an exception. For example, most functions in the
standard C library can be guaranteed not to throw an exception with
the notable exceptions of qsort and bsearch that take function pointer
arguments.

__attribute__((nothrow))在C中的含义是在gcc处回答的-nothrow属性用于什么用途? 。基本上,它是与C ++代码配合使用的,如果该函数永远不会调用引发异常的C ++代码,则可以使用它。


要回答有关"此函数是可能的取消点,因此未标记__THROW"的其他问题:这涉及多线程。您可以"取消"线程,但是直到到达取消点时,它才真正"取消"。一些更多信息:http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_cancel.3.html