我查看了一些gcc属性列表,发现了一个引起我注意的:

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. The nothrow attribute is not
implemented in GCC versions earlier than 3.3.

C函数如何引发异常?有人能解释一下这个属性的用途吗?
似乎有一个nothrow标签可用,但我发现它似乎与C++ std::nothrow有关。不确定这是否和我的问题有关。

最佳答案

这对于从C++代码调用C代码具有重要意义,它保证编译器在C代码不能抛出异常的情况下进行优化。这是有用的,如果它是C代码,不能抛出,或因为它是专门写的C++从未抛出异常。
所以,如果你写的是纯C,你永远都不需要这个,但是如果你认为某人可以把你的代码从C++调用为一个库,那么它是很方便的。

07-26 04:58