以下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语言绑定?
发布评论