typeof
来源:互联网 发布:中国男人非洲女人 知乎 编辑:程序博客网 时间:2024/05/05 17:32
container_of(ptr, type, member)中包含typeof关键字,我们在这分析一下typeof
typeof关键字是C语言中的一个新扩展。只要可以接受typedef名称,Sun Studio C 编译器就可以接受带有typeof的结构,包括以下语法类别:
- 声明
- 函数声明符中的参数类型链表和返回类型
- 类型定义
- 类型操作符s
- sizeof操作符
- 复合文字
- typeof实参
编译器接受带双下划线的关键字:__typeof和__typeof__。本文中的例子并没有遵循使用双下划线的惯例。从语句构成上看,typeof关键字后带圆括号,其中包含类型或表达式的名称。这类似于sizeof关键字接受的操作数(与sizeof不同的是,位字段允许作为typeof实参,并被解释为相应的整数类型)。从语义上看,typeof 关键字将用做类型名(typedef名称)并指定类型。
使用typeof的声明示例
下面是两个等效声明,用于声明int类型的变量a。
typeof(int) a; /* Specifies variable a which is of the type int */ typeof('b') a; /* The same. typeof argument is an expression consisting of character constant which has the type int */
以下示例用于声明指针和数组。为了进行对比,还给出了不带typeof的等效声明。
typeof(int *) p1, p2; /* Declares two int pointers p1, p2 */int *p1, *p2;typeof(int) * p3, p4;/* Declares int pointer p3 and int p4 */int * p3, p4;typeof(int [10]) a1, a2;/* Declares two arrays of integers */int a1[10], a2[10];
如果将typeof用于表达式,则该表达式不会执行。只会得到该表达式的类型。以下示例声明了int类型的var变量,因为表达式foo()是int类型的。由于表达式不会被执行,所以不会调用foo函数。
extern int foo();typeof(foo()) var;
使用typeof的声明限制
请注意,typeof构造中的类型名不能包含存储类说明符,如extern或static。不过允许包含类型限定符,如const或volatile。例如,下列代码是无效的,因为它在typeof构造中声明了extern:
typeof(extern int) a;下列代码使用外部链接来声明标识符b是有效的,表示一个int类型的对象。下一个声明也是有效的,它声明了一个使用const限定符的char类型指针,表示指针p不能被修改。
extern typeof(int) b;typeof(char * const) p = "a";在宏声明中使用typeof
typeof构造的主要应用是用在宏定义中。可以使用typeof关键字来引用宏参数的类型。因此,在没有将类型名明确指定为宏实参的情况下,构造带有所需类型的对象是可能的。
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof
- typeof()
- typeof()
- typeof
- typeof
- typeof
- typeof
- typeof
- The End-of-Line Story
- Oracle解锁
- SharePoint 2010 Server Error: The URL "XXX" is invalid, it may refer to a nonexistent file or ...
- hdu3466(01背包)
- python多线程并发执行程序实战讲解
- typeof
- 每日一题(50) - 绝对值最大的子序列和以及其区间
- libtool: link: `ext/standard/info.lo' is not a valid libtool object
- 最近开发Android的一些总结
- HDU 1002 A + B Problem II
- 戴维南定理
- svn co 默认密钥' GNOME keyring
- MYSQLMANAGER实例管理器总结
- 浅议柔性数组