__THROW是什么东西

62 篇文章 0 订阅

linux下直接看源码就行。

转自:https://blog.csdn.net/lanmanck/article/details/6856168

__THROW是什么东西?很多头文件里面对函数的声明后面都跟一个这东西,查了一下,有这么个文章说的清楚,转来看看。

Linux/FreeBSD 内核的源文件里常会出现这个东东。其实并不复杂,只是简单的宏定义,可以参考以下代码

<sys/cdefs.h>;:
/* GCC can always grok prototypes.  For C++ programs we add throw()
   to help it optimize the function calls.  But this works only with
   gcc 2.8.x and egcs.  */
# if defined __cplusplus && (__GNUC__ >;= 3 || __GNUC_MINOR__ >;= 
#  define __THROW       throw ()
# else
#  define __THROW
# endif
# define __P(args)      args __THROW
/* This macro will be used for functions which might take C++ callback
   functions.  */
# define __PMT(args)    args
# define __DOTS         , ...

像这个
static void icmp6_errcount __P((struct icmp6errstat *, int, int)); 
展开后就是
static void icmp6_errcount (struct icmp6errstat *, int, int) throw();
或者
static void icmp6_errcount (struct icmp6errstat *, int, int);
 
__P在c++调用的情况下,会在函数声明后加入 throw()
表明该函数不会扔出异常,这样避免编译器生成对异常的
处理代码,优化生成结果.

//==============================
个人觉得这个解析好点:

首先,__THROW宏是纯粹是linux平台上C库才有的东西,其他平台(如windows)上的C库里是不会有的。

在C里面,这个宏完全没有意义。当这个头文件被C++引用时,才有意义,其意义是声明这个函数支持C++里的throw异常功能。看下面一段说明:

__THROW is meant to declare the function as capable of
throwing exceptions (a C++ feature). In C, the macro does nothing.

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值