Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static member function with C language binding?

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)?

like image 497
Cheers and hth. - Alf Avatar asked Dec 02 '15 07:12

Cheers and hth. - Alf


2 Answers

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().

like image 85
Michael Burr Avatar answered Nov 15 '22 08:11

Michael Burr


A static member function has the same calling convention as a C function. But, name mangling applies. So, even if you get away with declaring your static member as extern "C", the linker would probably not find it when you try to link it against the C code that calls that function.

What you can easily do is declare a wrapper/stub that calls the static member from a plain function. Also, you can assign the static member function's address to a plain function pointer.

like image 41
Hellmar Becker Avatar answered Nov 15 '22 09:11

Hellmar Becker