C++中的restrict关键字是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/776283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:13:31  来源:igfitidea点击:

What does the restrict keyword mean in C++?

c++restrict-qualifier

提问by

I was always unsure, what does the restrict keyword mean in C++?

我总是不确定,restrict 关键字在 C++ 中是什么意思?

Does it mean the two or more pointer given to the function does not overlap? What else does it mean?

这是否意味着给函数的两个或多个指针不重叠?还有什么意思?

回答by Robert S. Barnes

In his paper, Memory Optimization, Christer Ericson says that while restrictis not part of the C++ standard yet, that it is supported by many compilers and he recommends it's usage when available:

Christer Ericson在他的论文Memory Optimization 中说,虽然restrict它还不是 C++ 标准的一部分,但它得到了许多编译器的支持,他建议在可用时使用它:

restrict keyword

! New to 1999 ANSI/ISO C standard

! Not in C++ standard yet, but supported by many C++ compilers

! A hint only, so may do nothing and still be conforming

A restrict-qualified pointer (or reference)...

! ...is basically a promise to the compiler that for the scope of the pointer, the target of the pointer will only be accessed through that pointer (and pointers copied from it).

限制关键字

!1999 ANSI/ISO C 标准的新增内容

!尚未符合 C++ 标准,但已被许多 C++ 编译器支持

!只是一个提示,所以可能什么都不做但仍然符合要求

限制限定的指针(或引用)...

!...基本上是对编译器的一个承诺,即对于指针的范围,指针的目标只能通过该指针(以及从中复制的指针)访问。

In C++ compilers that support it it should probably behave the same as in C.

在支持它的 C++ 编译器中,它的行为应该与在 C 中相同。

See this SO post for details: Realistic usage of the C99 ‘restrict' keyword?

有关详细信息,请参阅此 SO 帖子: Realistic usage of the C99 'restrict' 关键字?

Take half an hour to skim through Ericson's paper, it's interesting and worth the time.

花半个小时浏览 Ericson 的论文,这很有趣,值得花时间。

Edit

编辑

I also found that IBM's AIX C/C++ compiler supports the __restrict__keyword.

我还发现 IBM 的AIX C/C++ 编译器支持__restrict__关键字

g++ also seems to support this as the following program compiles cleanly on g++:

g++ 似乎也支持这一点,因为以下程序可以在 g++ 上干净地编译:

#include <stdio.h>

int foo(int * __restrict__ a, int * __restrict__ b) {
    return *a + *b;
}

int main(void) {
    int a = 1, b = 1, c;

    c = foo(&a, &b);

    printf("c == %d\n", c);

    return 0;
}

I also found a nice article on the use of restrict:

我还发现了一篇关于使用的不错的文章restrict

Demystifying The Restrict Keyword

揭秘 Restrict 关键字

Edit2

编辑2

I ran across an article which specifically discusses the use of restrict in C++ programs:

我看到一篇专门讨论在 C++ 程序中使用限制的文章:

Load-hit-stores and the __restrict keyword

Load-hit-stores 和 __restrict 关键字

Also, Microsoft Visual C++ also supports the __restrictkeyword.

此外,Microsoft Visual C++也支持__restrict关键字.

回答by dirkgently

Nothing. It was added to the C99 standard.

没有。它被添加到 C99 标准中。

回答by unwind

Thisis the original proposal to add this keyword. As dirkgently pointed out though, this is a C99feature; it has nothing to do with C++.

是添加此关键字的原始建议。不过正如 dirkgently 指出的,这是C99 的特性;它与 C++ 无关。

回答by AnT

There's no such keyword in C++. List of C++ keywords can be found in section 2.11/1 of C++ language standard. restrictis a keyword in C99 version of C language and not in C++.

C++ 中没有这样的关键字。C++ 关键字列表可以在 C++ 语言标准的第 2.11/1 节中找到。restrict是 C 语言的 C99 版本中的关键字,而不是 C++ 中的关键字。

回答by Johan Boulé

Since header files from some C libraries use the keyword, the C++ language will have to do something about it.. at the minimum, ignoring the keyword, so we don't have to #define the keyword to a blank macro to suppress the keyword.

由于来自某些 C 库的头文件使用了关键字,因此 C++ 语言将不得不对此做一些事情.. 至少,忽略关键字,因此我们不必将关键字 #define 为空白宏来抑制关键字.