__THROW是什么东西?

来源:互联网 发布:淘宝首页推广 编辑:程序博客网 时间:2024/04/28 13:47

__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 ofthrowing exceptions (a C++ feature). In C, the macro does nothing.
0 0
原创粉丝点击