相关疑难解决方法(0)

限制结构内的关键字和指针

通过使用这样的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不重叠?

c struct function restrict-qualifier

17
推荐指数
1
解决办法
1661
查看次数

我应该在引用上使用__restrict吗?

在我编写的程序中,我的一个函数声明如下:

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++ gcc reference visual-c++ restrict-qualifier

7
推荐指数
1
解决办法
2109
查看次数

在 C++ 中处理指针别名的确切规则是什么?

在 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关键字。

  1. 在标准定义的 C++ 中处理指针别名的确切规则是什么?
  2. 哪些流行的编译器扩展提供类似于restrictC 中的语义?例如MSVCg++clang++Intel C++ compiler
  3. restrict在未来的 C++ 标准中是否有任何计划要标准化的关键字?

c++ pointers strict-aliasing language-lawyer

5
推荐指数
0
解决办法
720
查看次数