双下划线(__const)是什么意思在C? [英] What does double underscore ( __const) mean in C?

查看:242
本文介绍了双下划线(__const)是什么意思在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  extern int ether_hostton(__const char * __ hostname,struct ether_addr * __ addr)
__THROW;

我在Linux上的/usr/include/netinet/ether.h中找到了上述函数定义



有人可以在const(关键字),addr(标识符)和最后一个__THROW之前解释双下划线的含义。


<在C中,以下划线开头,后跟大写字母或下划线的符号保留用于实现。作为C的用户,您不应创建以保留序列开头的任何符号。在C ++中,限制更严格;



给定:

 

code> extern int ether_hostton(__const char * __ hostname,struct ether_addr * __ addr)
__THROW;

__ const 符号是允许编译器使用这个代码支持原型符号但是没有正确理解C89标准关键字 const 的可能性(有点不太可能)。 autoconf 宏仍然可以检查编译器是否支持 const ;



使用 __ hostname __ addr 是对您,头的用户的保护措施。如果使用GCC和 -Wshadow 选项编译,编译器将在任何局部变量影响全局变量时发出警告。如果函数使用 hostname 而不是 __ hostname ,并且如果你有一个名为 hostname (),会有阴影。



使用 __ THROW 表示保留给实施的名称在某些情况下,代码可以用某种throw规范来声明。这不是标准C;它更像是C ++。但是代码可以与C编译器一起使用,只要其中一个头(或编译器本身)将 __ THROW 定义为空,或者某些编译器特定的扩展标准C语法。






C标准(ISO 9899:1999)的第7.1.3节说:


7.1.3保留的标识符



每个标题声明或定义其相关子条款中列出的所有标识符和
可选地声明或定义在其相关的未来库方向中列出的标识符
子句和始终为任何使用或用作文件
作用域标识符而保留的标识符。



- 以下划线开头,大写字母或另一个
下划线的所有标识符始终保留供任何使用。



- 以下划线开头的所有标识符始终保留用作标识符
,在普通和标记名称空间中都有文件范围。



- 每个在以下任何子条款(包括未来库
方向)中的宏名称如果包含其任何相关联的标题,则保留为指定使用;



- 在以下任何子条款中包含外部链接的所有标识符(包括
未来 154)



- 每个标识符的文件范围如下:任何以下子条款(包括
未来库指南)保留作为宏名称和作为标识符与
文件范围在同一名称空间中,如果包括任何相关联的头文件。 / p>

不保留其他标识符。如果程序在
上下文中声明或定义了一个标识符(除了7.1.4允许的),或者将一个保留的
标识符定义为宏名称,那么行为是未定义的。



如果程序删除($ #undef )任何第一个标识符的宏定义


脚注154)具有外部链接的保留标识符列表包括 errno math_errhandling
setjmp va_end







另请参阅;如果在C ++标识符中使用下划线有什么规则,则可以使用下面的命令:/ 228787/228787 /什么是关于使用下划线的规则。很多相同的规则适用于C和C ++,但是嵌入式双下划线规则仅在C ++中,如本答案顶部所述。


extern int ether_hostton (__const char *__hostname, struct ether_addr *__addr)
 __THROW;

I found the above function definition in /usr/include/netinet/ether.h on a Linux box.

Can someone explain what the double underscores mean in front of const (keyword), addr (identifier) and at last __THROW.

解决方案

In C, symbols starting with an underscore followed by either an upper-case letter or another underscore are reserved for the implementation. You as a user of C should not create any symbols that start with the reserved sequences. In C++, the restriction is more stringent; you the user may not create a symbol containing a double-underscore.

Given:

extern int ether_hostton (__const char *__hostname, struct ether_addr *__addr)
__THROW;

The __const notation is there to allow for the possibility (somewhat unlikely) that a compiler that this code is used with supports prototype notations but does not have a correct understanding of the C89 standard keyword const. The autoconf macros can still check whether the compiler has working support for const; this code could be used with a broken compiler that does not have that support.

The use of __hostname and __addr is a protection measure for you, the user of the header. If you compile with GCC and the -Wshadow option, the compiler will warn you when any local variables shadow a global variable. If the function used just hostname instead of __hostname, and if you had a function called hostname(), there'd be a shadowing. By using names reserved to the implementation, there is no conflict with your legitimate code.

The use of __THROW means that the code can, under some circumstances, be declared with some sort of 'throw specification'. This is not standard C; it is more like C++. But the code can be used with a C compiler as long as one of the headers (or the compiler itself) defines __THROW to empty, or to some compiler-specific extension of the standard C syntax.


Section 7.1.3 of the C standard (ISO 9899:1999) says:

7.1.3 Reserved identifiers

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).

— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.154)

— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

Footnote 154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and va_end.


See also What are the rules about using an underscore in a C++ identifier; a lot of the same rules apply to both C and C++, though the embedded double-underscore rule is in C++ only, as mentioned at the top of this answer.

这篇关于双下划线(__const)是什么意思在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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