通过使用这样的restrict关键字:
int f(int* restrict a, int* restrict b);
Run Code Online (Sandbox Code Playgroud)
我可以指示编译器数组a和b不重叠.说我有一个结构:
struct s{
(...)
int* ip;
};
Run Code Online (Sandbox Code Playgroud)
并编写一个带有两个struct s对象的函数:
int f2(struct s a, struct s b);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我怎样才能类似地指示编译器a.ip并且b.ip不重叠?
在我编写的程序中,我的一个函数声明如下:
bool parse( const sentence & __restrict sentence )
{
// whatever
}
Run Code Online (Sandbox Code Playgroud)
当我使用Microsoft Visual Studio 2010 Express编译代码时,编译器会抱怨:
警告C4227:使用的时间错误:引用的限定符被忽略
但是,GCC文档的这一页说:
除了允许受限制的指针之外,您还可以指定受限制的引用,这些引用指示引用在本地上下文中没有别名.
同一页面给出了一个非常明确的例子:
void fn (int *__restrict__ rptr, int &__restrict__ rref)
{
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
我是否误解了MVSC的警告?或者我应该将我的所有引用转换为指针以便__restrict适用?
在 C 中有一个restrict关键字告诉编译器在函数的指针参数之间没有别名,允许它以这种方式执行一些否则将不允许的优化。例如:
void add(int* restrict ptrA,
int* restrict ptrB,
int* restrict val)
{
*ptrA += *val;
*ptrB += *val;
}
Run Code Online (Sandbox Code Playgroud)
现在函数体中的指令可以并行执行,因为val和 某些ptr参数之间没有别名。在 C++ 中没有restrict关键字。
restrictC 中的语义?例如MSVC、g++、clang++和Intel C++ compiler。restrict在未来的 C++ 标准中是否有任何计划要标准化的关键字?