On 2/2/2020 8:11 AM, Liu Hao wrote:
在 2020/2/1 下午7:06, Edward Diener 写道:
Given the code:
class cbase;
int main()
{
typedef int __attribute__ ((__stdcall__)) (cbase::* atype)();
typedef int __attribute__ ((__cdecl__)) (cbase::* btype)();
typedef int __attribute__ ((__fastcall__)) (cbase::* ctype)();
typedef int __attribute__ ((__thiscall__)) (cbase::* dtype)();
return 0;
}
If I compile this for x64 (-m64) in gcc-9.2 I receive no errors or
warnings. Yet
https://gcc.gnu.org/onlinedocs/gcc/x86-Function-Attributes.html tells me
that all four are only for x86-32 targets. Is the documentation wrong,
is this a bug in gcc-9.2, or have I missed something ?
Are you compiling for Windows targets? If you target x64 Windows then
it's likely that such attributes are accepted but ignored in line with
MS compilers.
On Windows in a 64-bit compile the attributes are ignored.
On Linux in a 64-bit compile the attributes are ignored but at least
there are indeed warning messages saying so. But the do9c should explain
this.
In both Windows and Linux in a 32-bit compile I get messages such as:
test_calling_conventions.cpp: In function 'int main()':
test_calling_conventions.cpp:5:62: error: stdcall and cdecl attributes
are not compatible
5 | typedef int __attribute__ ((__cdecl__)) (cbase::* btype)();
| ^
test_calling_conventions.cpp:6:65: error: fastcall and cdecl attributes
are not compatible
6 | typedef int __attribute__ ((__fastcall__)) (cbase::* ctype)();
| ^
test_calling_conventions.cpp:6:65: error: fastcall and stdcall
attributes are not compatible
test_calling_conventions.cpp:7:65: error: stdcall and thiscall
attributes are not compatible
7 | typedef int __attribute__ ((__thiscall__)) (cbase::* dtype)();
| ^
test_calling_conventions.cpp:7:65: error: fastcall and thiscall
attributes are not compatible
test_calling_conventions.cpp:7:65: error: cdecl and thiscall attributes
are not compatible
This does not make sense to me as the compiler seems to be objecting to
just having different pointer to member function types with different
calling conventions for the same class, as if all member functions for a
class must have the same calling conventions. Does gcc not allow
different calling conventions for different member functions of a class,
and, if so, where is this documented ?