您的位置:首页 > 编程语言 > C语言/C++

C++中restrict修饰符作用

2016-03-19 15:45 316 查看
今天查看POSIX的API时候,发现里面有一行

int pthread_create(pthread_t * restrict thread,const pthread_attr_t *restrictattr,void *(*start_routine)(void*),void*restrict arg);
开始和不解为什么变量前面要加上restrict修饰符呢?restrict是

限制,限定; 约束,束缚
最后查询资料方得知:

restrict的定义是It can be applied only to pointers, and it indicates that a pointer is the sole initial means of
accessing a data object.



restrict是一个限定符,这个关键字据说来源于古老的FORTRAN,主要用来修饰指针指向的内存不能被别的指针引用。

restrict同样可以用于数组的声明:

If you specify type qualifiers such as void foo(int * restrict a);, the C compiler expresses it with array syntax void foo(int a[restrict]); which is essentially the same as declaring a restricted pointer.

其实restrict同const或valiate一样是一个修饰符而已,告诉编译器被restrict修饰的指针所指 向的对象,只能通过这个指针或基于这个指针的其他指针进行操作,即限制访问用restrict限制的指针指向的对象只能通过这个指针访问,这对编译器的优 化很有好处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: