简体   繁体   English

在哪些情况下,将限制限定符应用于返回值会产生影响?

[英]In which cases will the restrict qualifier applied to a return value have an effect?

If I have a member function declared like so: 如果我有这样声明的成员函数:

double* restrict data(){
    return m_data; // array member variable
}

can the restrict keyword do anything? limit关键字可以做什么?

Apparently, with g++ (x86 architecture) it cannot , but are there other compilers/architectures where this type of construction makes sense, and would allow for optimized machine code generation? 显然,对于g ++(x86体系结构)来说它不能实现 ,但是是否存在其他编译器/体系结构可以使这种类型的构造有意义,并且可以优化机器代码的生成?

I'm asking because the Blitz library (Blitz++) has a whole slew of functions declared in this manner, and it doesn't make sense that someone would go in and add the restrict keyword unless it actually does something. 我之所以问是因为Blitz库(Blitz ++)具有以这种方式声明的全部功能,因此除非有人真正执行某项操作,否则有人进入并添加strict关键字是没有意义的。 So before I go in and remove the restrict 's (to get rid of compiler warnings) I'd like to know how I'm abusing the code. 因此,在我删除restrict (消除编译器警告)之前,我想知道如何滥用代码。

WHAT restrict ARE WE TALKING ABOUT? 我们要谈什么restrict

restrict is, as it currently stands, non-standard.. which means that it's a compiler extension; 目前, restrict是非标准的。这意味着它是编译器扩展; it's non-portable in the sense that the C++ Standard doesn't mandate its existance, nor is there any formal text in it that tells us what it is supposed to do. 从C ++标准不要求其存在的意义上说,它是不可移植的,在它里面也没有任何正式的文本可以告诉我们它应该做什么。

restrict is currently compiler specific in C++, and one has to resort to the compiler documentation of their choice to see exactly what it is doing. restrict当前是C ++中特定于编译器的,必须依靠他们选择的编译器文档才能确切了解它在做什么。


SOME THOUGHTS 一些想法

There are many papers about the usage of restrict , among them: 以下是有关使用多篇论文restrict ,其中包括:

It's hinted at several places that the purpose of restrict is to qualify pointers so that the compiler knows that two pointers in the same scope doesn't refer to the same memory location. 在几个地方暗示restrict的目的是限定指针,以便编译器知道相同作用域中的两个指针不指向相同的内存位置。

With this in mind we can easily see that the return-type has no potential collision with other pointers, so using it in such context will generally not gain any optimization opportunities. 考虑到这一点,我们可以很容易地看到return类型与其他指针没有潜在的冲突 ,因此在这种情况下使用它通常不会获得任何优化机会。 However; 然而; one must refer to the documented behaviour of the used implementation to know for sure.. as stated: restrict is not standard, yet . 一个必须参照使用实施记录的行为肯定知道。至于说: restrict不达标,


I also found the following thread where the developers of Blitz++ discusses the removal of strict applied to the return-type of a function, since it doesn't do anything: 我还找到了以下线程, Blitz ++的开发人员在该线程上讨论了删除对函数的返回类型应用的strict ,因为它没有任何作用:


A LITTLE NOTE 小笔记

As a further note, here's what the LLVM Documentation says about noalias vs restrict : 作为进一步的说明,这里是什么LLVM文件说,有关noalias VS restrict

For function return values, C99's restrict is not meaningful, while LLVM's noalias is. 对于函数返回值,C99的限制没有意义,而LLVM的noalias是有意义的。

Generaly restrict qualifier can only help to better optimize code. 通常,限制限定符只能帮助更好地优化代码。 By removing 'restrict' you don't break anything, but when you add it without care you can get some errors. 通过删除“限制”,您不会破坏任何内容,但是如果不加小心地添加它,则会出现一些错误。 A great example is the difference between memcpy and memmove. 一个很好的例子是memcpy和memmove之间的区别。 You can always use slower memmove, but you can use faster memcpy only if you know that src and dst aren't overlaping. 您始终可以使用速度较慢的记忆,但只有在知道src和dst不重叠的情况下,才可以使用速度较快的记忆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么返回类型上的“const”限定符对标有 __forceinline/inline 的函数没有影响? - Why does the 'const' qualifier on return type have no effect on functions that are marked with __forceinline/inline? 在哪些情况下 printf 返回负值? - In which cases does printf return a negative value? 在哪些情况下(const)引用返回值是不安全的? - In which cases is it unsafe to (const) reference a return value? 使用Rcpp构建R包,其中包含带有限制限定符的C源和头? - Building an R package with Rcpp which contains C source and header with restrict qualifier? 如何在此指针上应用restrict限定符 - How to apply restrict qualifier on this pointer 限制成员函数的限定符(限制此指针) - restrict qualifier on member functions (restrict this pointer) C ++在数组引用上限制限定符 - C++ restrict qualifier on array reference std :: move如何应用于函数的返回值? - How std::move applied on function return value? 在C ++命名空间中,当在标头中声明的非成员子例程加上前缀时,“ static”限定符是否有效? - In a C++ namespace does the `static` qualifier have any effect when prefixing non-member subroutines declared in the header? 为什么`exact`限定符没有生效? - Why does `precise` qualifier not take effect?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM