外部C不能在类级别使用? [英] extern C can not be used at class level?

查看:126
本文介绍了外部C不能在类级别使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想在Windows环境下确认,VSTS 2008 + C ++项目,我们可以仅适用于外部C运作水平,无法适用于类级别(这样从类用C语言名称重整所有成员函数) ?我曾尝试多种方法,但总是编译错误。

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 functions from the class use C language name mangling)? I have tried several ways, but always compile error.

在此先感谢,
乔治

thanks in advance, George

推荐答案

您可以排序通过一个非常令人费解的适用的externC成员函数(而是完全法律)黑客:

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

这根据是可能的ISO C ++ 9.3 03 [class.mfct] / 9:

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

一个成员函数可以使用函数类型的typedef声明(但不定义)。所得成员函数具有完全相同的类型,因为它会具有如果显式提供说明符的功能,参见8.3.5。

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.

不过,这并没有真正给你买东西,因为ISO C ++ 03 7.5 dcl.link / 4:

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

一个C语言联动忽略类成员的名字和成员函数
  类型的类成员函数。

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

这篇关于外部C不能在类级别使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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