静态成员函数与C语言绑定? [英] static member function with C language binding?

查看:229
本文介绍了静态成员函数与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语言绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆