如何在此指针上应用限制限定符
How to apply restrict qualifier on this pointer
如何将 GCC's/Clang 的 __restrict__
限定符应用到 class 的 this
指针?
这个问题的灵感来自 Richard Powell 的 CppCon 2018 演讲,“How to Argue(ment)." I saw a similar question "restrict qualifier on member functions (restrict this pointer)." (All code can be found on Compiler Explorer)
void bar();
class Foo {
public:
int this_example() const {
if (value > 0) {
bar();
return value;
} else {
return value;
}
}
private:
int value;
};
以上代码生成以下程序集。在其中,我们可以看到 value
必须通过 this
指针加载两次。这是有道理的,这是 C++ 继承自 C 和 restrict qualifier 允许程序员关闭该行为的结果。我找不到为 this
指针启用 restrict
功能的方法。
Foo::this_example() const: # @Foo::this_example() const
push rbx
mov eax, dword ptr [rdi]
test eax, eax
jle .LBB2_2
mov rbx, rdi
call bar()
mov eax, dword ptr [rbx]
.LBB2_2:
pop rbx
ret
在 Compiler Explorer 页面上,我展示了使用 __restrict__
来省略第二个加载的方法参数示例。还有一个将结构引用传递给函数并使用 __restrict__
省略第二个加载的示例。
我可以想象一个编译器允许程序员在方法参数中提及隐式 this
指针的世界。然后,编译器可以允许将限定符应用于 this
指针。有关示例,请参见下面的代码。
class Foo {
public:
int unrestricted(Foo *this);
int restricted(Foo *__restrict__ this);
};
作为后续问题,C++ 标准或 C++ 指南中是否有某些内容可以使 this
永远不会有 restrict 限定符?
GCC's documentation for __restrict__
(以及链接的问题)提到您实际上可以限制 this
:
You may also specify whether a member function’s this
pointer is unaliased by using __restrict__
as a member function qualifier.
void T::fn () __restrict__
{
/* … */
}
Within the body of T::fn
, this
has the effective definition T *__restrict__ const this
. Notice that the interpretation of a __restrict__
member function qualifier is different to that of const
or volatile
qualifier, in that it is applied to the pointer rather than the object. This is consistent with other compilers that implement restricted pointers.
但是请注意,这样标记 this
指针并不会阻止第二次加载。
如何将 GCC's/Clang 的 __restrict__
限定符应用到 class 的 this
指针?
这个问题的灵感来自 Richard Powell 的 CppCon 2018 演讲,“How to Argue(ment)." I saw a similar question "restrict qualifier on member functions (restrict this pointer)." (All code can be found on Compiler Explorer)
void bar();
class Foo {
public:
int this_example() const {
if (value > 0) {
bar();
return value;
} else {
return value;
}
}
private:
int value;
};
以上代码生成以下程序集。在其中,我们可以看到 value
必须通过 this
指针加载两次。这是有道理的,这是 C++ 继承自 C 和 restrict qualifier 允许程序员关闭该行为的结果。我找不到为 this
指针启用 restrict
功能的方法。
Foo::this_example() const: # @Foo::this_example() const
push rbx
mov eax, dword ptr [rdi]
test eax, eax
jle .LBB2_2
mov rbx, rdi
call bar()
mov eax, dword ptr [rbx]
.LBB2_2:
pop rbx
ret
在 Compiler Explorer 页面上,我展示了使用 __restrict__
来省略第二个加载的方法参数示例。还有一个将结构引用传递给函数并使用 __restrict__
省略第二个加载的示例。
我可以想象一个编译器允许程序员在方法参数中提及隐式 this
指针的世界。然后,编译器可以允许将限定符应用于 this
指针。有关示例,请参见下面的代码。
class Foo {
public:
int unrestricted(Foo *this);
int restricted(Foo *__restrict__ this);
};
作为后续问题,C++ 标准或 C++ 指南中是否有某些内容可以使 this
永远不会有 restrict 限定符?
GCC's documentation for __restrict__
(以及链接的问题)提到您实际上可以限制 this
:
You may also specify whether a member function’s
this
pointer is unaliased by using__restrict__
as a member function qualifier.void T::fn () __restrict__ { /* … */ }
Within the body of
T::fn
,this
has the effective definitionT *__restrict__ const this
. Notice that the interpretation of a__restrict__
member function qualifier is different to that ofconst
orvolatile
qualifier, in that it is applied to the pointer rather than the object. This is consistent with other compilers that implement restricted pointers.
但是请注意,这样标记 this
指针并不会阻止第二次加载。