静态成员函数与C语言绑定?

编程入门 行业动态 更新时间:2024-10-27 01:32:34
本文介绍了静态成员函数与C语言绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下C ++代码使用Visual C ++和g ++编译:

struct S { static void foo(); }; externC void S :: foo(){} struct T { static void foo }; externC void T :: foo(){} auto main() - > int { S()。foo(); T()。foo(); }

  • p>

  • 如果它是有效的,因为实现可能在单独的翻译单元中,这意味着静态成员函数总是具有与C函数如果没有,它怎么不暗示)?

解决方案

++ 11 7.5 / 4链接规范

AC语言链接在确定的语言链接时被忽略类成员和类成员函数的函数类型。

因此,您的示例在其未格式化的意义上是有效的或错误,但 externC对 S :: foo()或 T :: foo()。

The following C++ code compiles with Visual C++ and g++:

struct S { static void foo(); }; extern "C" void S::foo() {} struct T { static void foo(); }; extern "C" void T::foo() {} auto main() -> int { S().foo(); T().foo(); }

  • Is it valid?

  • If it's valid, since the implementation may be in a separate translation unit, does that imply that a static member function always has the same calling convention as a C function (and if not, how does it not imply that)?

解决方案

C++11 7.5/4 "Linkage specifications"

A C language linkage is ignored in determining the language linkage of the names of class members and the function type of class member functions.

So your example is valid in the sense that it's not malformed or an error, but the extern "C" should have no effect on S::foo() or T::foo().

更多推荐

静态成员函数与C语言绑定?

本文发布于:2023-08-05 11:21:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1305204.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   静态   函数   成员   语言

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!