我想知道是否可能仅在函数定义中包含restrict
关键字,而不是在函数声明中像这样:
void foo(char *bar);
void foo(char * restrict bar)
{
// do something
}
foo
只接受一个参数,任何指针别名都必须在 foo
内部发生。调用函数的人不需要知道 restrict
修饰符。在函数声明中省略该关键字是否可行,就像使用 const
一样?
void foo(void * restrict *a);
和 void foo(void **a);
声明了不同的函数类型。const int *
传递给期望int *
的函数,但我们可以传递int * const
,因为这是赋值规则规定的方式。 - Lundin__attribute__((noinline))
int process(const int * restrict const a, int * const b) {
*b /= (*a + 1) ;
return *a + *b ;
}
int main(void) {
int data[2] = {1, 2};
return process(&data[0], &data[0]);
}
使用restrict限定符时,软件以代码1终止,而不使用restrict限定符时则以0终止。
编译使用gcc -std=c99 -Wall -pedantic -O3 main.c
进行。
标志-O1
也可以完成任务。
等等。
int fred[42]; foo(fred)
,如果foo()
操作全局的fred[]
,那么这个调用就违反了restrict
的限制。 - chux - Reinstate Monicachar *p = bar
这样的语句就没问题。 - ericw31415restrict
是为编译器而设,但在声明中参数上使用restrict
则是为了程序员,编译器会忽略它。 - chqrlie