gcc - attribute nothrow用于什么?

6

我正在查看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相关。不确定是否与我的问题相关。


2
gcc也是一个C++编译器。 - r3mainer
1
我认为“异常”可能被用作“陷阱条件”的同义词,例如除以零。int dd(int x, int y) { if (!y) return 0; return x/y; } - pmg
1个回答

4

在从C++代码调用C代码时,这很重要,它保证编译器进行优化时C代码不会抛出异常。如果要么是C代码且不能抛出异常,要么是特意编写的C++代码不会抛出异常,则这很有用。

因此,如果您正在编写纯C代码,则永远不需要使用此功能;但是,如果您认为其他人可能会将您的代码作为库从C++中调用,则这很方便。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接