问题描述
我正在更新性能关键库以使用restrict,如在C ++ 11中通过g ++和MSVC使用关键字 __ restrict
实现的。这似乎是最标准的扩展,所以我将使用 restrict
和 __ restrict
可互换。
restrict
是一个C99关键字,但是编译器在C ++中定义了它的重要用途。
这个帖子是一个问题,询问每个C ++的具体使用是什么,它的意思,后面是一个CW答案回答。随意添加/检查/编辑。所以:帮助!这些C ++使用 restrict
关键字是什么意思?
-
合格
void Foo :: method(int * __ restrict a)__restrict {/*...*/}
-
限制参考:
int& __ restrict x = /*...*/;
-
限制模板内部:
std :: vector< float * __ restrict> X;
-
限制成员/字段。这在技术上也适用于C的
struct
,但它在C ++中作为一个问题比它在C中更频繁:class Foo final {public:int * __ restrict field; };
-
合格
此
(限制方法):
此
指针被限制。这一个主要后果:
-
方法无法作为数据操作,例如:
void Foo :: method(Foo * __ restrict other)__restrict {/*...*/}
在这个例子中,
this
可能别名other
。restrict
表示您不能以自身作为参数调用此方法。 -
即使通过字段,也可以 访问或更改对象。原因是以下是功能相同的:
void Foo :: method1(void)__restrict {field = 6; }
void Foo :: method2(void)__restrict {this-> field = 6; }
在这个例子中,
this
别名任何东西。
-
-
限制参考:
它似乎意味着 - 引用受到限制。这究竟是什么,以及它是否有用是另一回事。 声明编译器可以静态确定引用的别名,所以关键字据说是无用的。 也被问及是否应该使用,但答案
-
。简而言之,在函数
f
中,编译器知道a.field
和b.field
没有别名:class Foo final {
int * __ restrict field;
};
int f(Foo a,Foo b){/*...*/}
这通常是这样的,假设
a!= b
- 例如,如果字段被Foo的构造函数/析构函数分配和销毁。注意,如果field是一个原始数组,它总是为真,因此restrict
关键字是不必要的()。
I'm in the process of updating performance critical libraries to use restrict, as implemented in C++11 by g++ and MSVC with the keyword __restrict
. This seems to be the most-standard-extension, so I'll use restrict
and __restrict
interchangeably.
restrict
is a C99 keyword, but nevertheless compilers have defined important uses for it in C++.
This post intends to be a "question" asking about what each C++-specific use is and what it means, followed by a CW answer answering it. Feel free to add/check/edit. So: "Help! What do these C++ uses of the restrict
keyword mean?"
Qualifying
this
(restrict a method):void Foo::method(int*__restrict a) __restrict { /*...*/ }
Restrict a reference:
int&__restrict x = /*...*/;
Restrict inside a template:
std::vector<float*__restrict> x;
Restrict a member/field. This technically also applies to C's
struct
, but it comes up as an issue in C++ more often than it does in C:class Foo final { public: int*__restrict field; };
Qualifying
this
(restrict a method):This means that the
this
pointer is restricted. This one major consequence:The method can't operate on itself as data, e.g.:
void Foo::method(Foo*__restrict other) __restrict { /*...*/ }
In that example,
this
might otherwise aliasother
.restrict
is saying that you can't call this method with itself as an argument.Note: it is okay to access or change the object, even through a field. The reason why is that the following are functionally identical:
void Foo::method1(void) __restrict { field=6; } void Foo::method2(void) __restrict { this->field=6; }
In that example,
this
is not aliased with anything.
Restrict a reference:
It appears to mean exactly that--that the reference is restricted. What this exactly does and whether it's useful is another matter. Someone on this thread claims compilers can statically determine aliasing for references, and so the keyword is supposedly useless. This question was also asked about whether it should be used, but the answer "vendor specific" is hardly helpful.
There is precedent on this question. In short, in function
f
, the compiler knows thata.field
andb.field
are not aliased:class Foo final { int*__restrict field; }; int f(Foo a, Foo b) { /*...*/ }
This will often be the case, assuming
a!=b
--for example, if field is allocated and destroyed by the constructor/destructor of Foo. Note that if field is a raw array, it will always be true and so therestrict
keyword is unnecessary (and impossible) to apply.
这篇关于C ++限制语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!