__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);
<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.
理解Linux内核中__THROW宏的作用
本文深入解析了Linux/FreeBSD内核源文件中常见宏__THROW的功能,及其在C++调用场景下的作用。通过具体代码示例,解释了如何使用__THROW宏来优化函数调用并避免异常处理代码的生成。
9032

被折叠的 条评论
为什么被折叠?



