__thiscall 关键字的用途是什么?
Whats the purpose of the __thiscall keyword?
通读 calling conventions 时有一个 thiscall
约定,其目的非常明确。
我知道 thiscall
定义了一个函数,它是 class 的一部分,因为这些函数需要一个对象作为非静态函数的第一个隐藏参数。
所以如果我没理解错,那么下面的函数就一定是thiscall
。不需要使用关键字,因为编译器在那里没有太多选择,必须这样声明。
class Foo
{
public:
int function(void) { return 1; }
};
现在,当我像这样在 class 之外声明函数时:
int __thiscall Otherfunction(void) { return 1; }
int __thiscall Otherfunction(Foo *t) { return 1; }
然后我得到编译器错误:
error C3865: '__thiscall': can only be used on native member functions
它甚至是有道理的,因为您将如何调用这些函数?那么为什么不能使用这个关键字甚至存在呢?或者是否有一些使用此 can/has 的用例?
these functions require an object as their first hidden parameter for non-static functions
具有讽刺意味的是,你自己回答了一半,因为你在这里描述的是 fastcall
,而不是 thiscall
,这是调用成员函数的古老替代方法。 thiscall
将 this
存储在寄存器中,而不是堆栈中。
这就是你的答案,不是因为 thiscall
可以用于非成员函数,而是因为你可以对成员函数使用不同的约定(fastcall
当然还有 nakedcall
或者随便叫什么)。
来自官方 __thiscall
文档。
On ARM, ARM64, and x64 machines, __thiscall
is accepted and ignored by the compiler. That's because they use a register-based calling convention by default.
One reason to use __thiscall
is in classes whose member functions use __clrcall
by default. In that case, you can use __thiscall
to make individual member functions callable from native code.
When compiling with /clr:pure
, all functions and function pointers are __clrcall
unless specified otherwise. The /clr:pure
and /clr:safe
compiler options are deprecated in Visual Studio 2015 and unsupported in Visual Studio 2017.
这似乎意味着在 ARM[64] 或 x86 上编写纯 C++(非托管)代码或在 VC+ 中编写 C++/CLI 代码时,永远不需要明确指定 __thiscall
+ 2017 及更高版本。
正如我发现的那样,__fastcall
有意义的用例是,当默认调用约定已被编译器选项覆盖并且您需要具有使用特定调用约定的函数时:
class Foo
{
public:
int __fastcall function(void) { return 1; }
};
通读 calling conventions 时有一个 thiscall
约定,其目的非常明确。
我知道 thiscall
定义了一个函数,它是 class 的一部分,因为这些函数需要一个对象作为非静态函数的第一个隐藏参数。
所以如果我没理解错,那么下面的函数就一定是thiscall
。不需要使用关键字,因为编译器在那里没有太多选择,必须这样声明。
class Foo
{
public:
int function(void) { return 1; }
};
现在,当我像这样在 class 之外声明函数时:
int __thiscall Otherfunction(void) { return 1; }
int __thiscall Otherfunction(Foo *t) { return 1; }
然后我得到编译器错误:
error C3865: '__thiscall': can only be used on native member functions
它甚至是有道理的,因为您将如何调用这些函数?那么为什么不能使用这个关键字甚至存在呢?或者是否有一些使用此 can/has 的用例?
these functions require an object as their first hidden parameter for non-static functions
具有讽刺意味的是,你自己回答了一半,因为你在这里描述的是 fastcall
,而不是 thiscall
,这是调用成员函数的古老替代方法。 thiscall
将 this
存储在寄存器中,而不是堆栈中。
这就是你的答案,不是因为 thiscall
可以用于非成员函数,而是因为你可以对成员函数使用不同的约定(fastcall
当然还有 nakedcall
或者随便叫什么)。
来自官方 __thiscall
文档。
On ARM, ARM64, and x64 machines,
__thiscall
is accepted and ignored by the compiler. That's because they use a register-based calling convention by default.One reason to use
__thiscall
is in classes whose member functions use__clrcall
by default. In that case, you can use__thiscall
to make individual member functions callable from native code.When compiling with
/clr:pure
, all functions and function pointers are__clrcall
unless specified otherwise. The/clr:pure
and/clr:safe
compiler options are deprecated in Visual Studio 2015 and unsupported in Visual Studio 2017.
这似乎意味着在 ARM[64] 或 x86 上编写纯 C++(非托管)代码或在 VC+ 中编写 C++/CLI 代码时,永远不需要明确指定 __thiscall
+ 2017 及更高版本。
正如我发现的那样,__fastcall
有意义的用例是,当默认调用约定已被编译器选项覆盖并且您需要具有使用特定调用约定的函数时:
class Foo
{
public:
int __fastcall function(void) { return 1; }
};