在c中可以用不同的调用约定定义函数吗?
int _cdecl f (int x) { return 0; }
int _stdcall f (int y) { return 0; }
名称修改后将是:
_f
_f@4
哪个不冲突,这在 c 中允许吗?如果不允许,为什么?
int _cdecl f (int x) { return 0; }
int _stdcall f (int y) { return 0; }
After name mangling will be:
_f
_f@4
Which doesn't conflict, is this allowed in c ,if not, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键字
_cdecl
和_stdcall
不是 C 语言的一部分。这些是 Microsoft 扩展,之前有类似的 Borland 扩展。在标准 C 语言中,您无法声明调用约定。显然,声明的每个函数都等效于 MS 编译器所指的“
_cdecl
”约定。当您调用这两个函数时,可以使用内联汇编来区分它们。由于您使用的是特定于平台的 C 供应商扩展,因此您可能会考虑使用内联汇编。
The keywords
_cdecl
and_stdcall
are not part of the C language. These are Microsoft extensions which were preceded by similar Borland extensions.In the standard C language, you can't declare a calling convention. Every function declared is, obviously, equivalent to what the MS compiler refers to as the "
_cdecl
" convention.It would be possible to use in-line assembly to distinguish the two functions when you call them. Because you're using a platform-specific vendor extension of C, you might consider using in-line assembly.
首先,这不是破坏,而是装饰。重整是 C++ 编译器中会发生的事情,因为 C++ 最初设计为支持使用 C 样式链接工具的重载。
至于你的问题,你不能有两个同名的函数。为了应用该规则,使用未修饰的名称。
为什么会这样呢?我想这是因为装饰和调用约定不是 C 标准的一部分,并且是特定于每个编译器的。我非常确定,支持多种调用约定的 C 编译器是在 C 发明多年后才出现的。
First off, that's not mangling, that's decoration. Mangling is something that happens with C++ compilers because C++ was originally designed to to support overloading using C style link tools.
As to your question, you can't have two functions with the same name. For the purposes of applying that rule, the un-decorated name is used.
Why is this so? I'd imagine it is because decoration and calling conventions are not part of the C standard and are specific to each compiler. I'm pretty sure that C compilers supporting multiple calling conventions only came in to being a number of years after C was invented.