C++中restrict修饰符作用

来源:互联网 发布:ubuntu怎么重新分区 编辑:程序博客网 时间:2024/06/03 12:47

今天查看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限制的指针指向的对象只能通过这个指针访问,这对编译器的优 化很有好处。



0 0
原创粉丝点击