extern C can not be used at class level?

前端 未结 5 1214
故里飘歌
故里飘歌 2020-12-06 02:22

Just want to confirm in Windows environment, VSTS 2008 + C++ project, we could only apply extern C to function level, not be able to apply to class level (so that all member

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 03:06

    You can sort of apply extern "C" to a member function via a very convoluted (but entirely legal) hack:

    extern "C" typedef int bar_t(int x);
    
    struct foo {
         bar_t bar; // yes, this declares a nonstatic member function!
    };
    
    int foo::bar(int x) { return x; } // definition
    

    This is possible according to ISO C++03 9.3[class.mfct]/9:

    a member function can be declared (but not defined) using a typedef for a function type. The resulting member function has exactly the same type as it would have if the function declarator were provided explicitly, see 8.3.5.

    However, this doesn't really buy you anything, because of ISO C++03 7.5[dcl.link]/4:

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

提交回复
热议问题