gcc - 什么是属性nothrow用于? [英] gcc - what is attribute nothrow used for?

查看:142
本文介绍了gcc - 什么是属性nothrow用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  nothrow 
nothrow属性用于通知编译器一个函数不能
抛出异常。例如,标准C库中的大多数函数可以保证
不会抛出一个异常,并带有函数指针参数的qsort和
bsearch的显着异常。 nothrow属性不是在3.3以前的GCC版本中实现的

C函数如何引发异常?有人可以解释这个属性用于什么?



似乎有一个 nothrow 标签可用,我发现这似乎与C ++ std :: nothrow 有关。不知道这是否与我的特定问题有关。

解决方案

从C ++代码调用C代码时,这具有重要意义,编译器的优化目的是C代码不能抛出异常。这是非常有用的,如果它是C代码并且不能抛出,或者因为它是C ++专门编写的,永远不会抛出异常。

因此最终如果你正在编写纯C你应该永远不需要这个,但是如果你认为有人可能会把你的代码从C ++作为一个库调用,那么它可以很方便。


I was taking a look through some of the gcc attributes list and I spotted this one that caught my attention:

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. 

How could a C function throw an exception? Could someone explain what this attribute is used for?

There seems to be a nothrow tag available but what I've found there seem to be related to C++ std::nothrow. Not sure whether that's related to my particular question.

解决方案

This has significance when calling C code from C++ code, it guarantees to the compiler for optimization purposes that the C code cannot throw exceptions. This is useful if either it is C code and cannot throw, or because it is C++ written specifically to never throw up an exception.

So ultimately if you are writing pure C you should never need this, however if you think someone may call your code from C++ as a library it can be handy.

这篇关于gcc - 什么是属性nothrow用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆