C ++引用的参数和C联动 [英] C++ by-reference argument and C linkage

查看:213
本文介绍了C ++引用的参数和C联动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经遇到过一个工作(与XLC8和MSFT9编译器)件code,含有与C键和一个引用参数定义的函数的C ++文件。这个错误我,因为引用仅限于C ++。有问题的功能是从C code,其中它被声明为代替引用参数取指针参数为相同的类型,称为

I have encountered a working (with XLC8 and MSFT9 compilers) piece of code, containing a C++ file with a function defined with C linkage and a reference argument. This bugs me, as references are C++ only. The function in question is called from C code, where it is declared as taking a pointer argument to the same type in place of the reference argument.

简单的例子

C ++文件的:

extern "C" void f(int &i)
{
    i++;
}

C文件的:

void f(int *);

int main()
{
    int a = 2;
    f(&a);
    printf("%d\n", a);  /* Prints 3 */
}

现在,街道上的字是大多数C ++编译器,引擎盖下,实施就像一个指针引用。是不是这样的,只是纯粹的运气这个code工程或它的某个地方在C ++规范的结果是什么说什么时候你定义一个基准参数和C联动功能的原因吗?我一直没能找到这方面的消息。

Now, the word on the street is that most C++ compilers, under the hood, implement references just like a pointer. Is it like that and just pure luck the reason this code works or does it say somewhere in the C++ specification what the result is when you define a function with a reference argument and C linkage? I haven't been able to find any information on this.

推荐答案

我n3000.pdf的复印件(的这里),有这样一段在第7.5 MDASH说; 联动规格的:

My copy of n3000.pdf (from here), has this to say in section 7.5—Linkage specifications:

9 的。联动从C ++中定义的对象
  其他语言和对象定义
  在C ++中从其他语言的
  实现定义和
  与语言相关的。只有在
  两个对象布局策略
  语言实现类似
  足够可以这样联动来实现。

9. Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved.

由于C和C ++是不同的语言,这意味着你不能依靠这个共同的编译器的功能。

Since C and C++ are different languages, this means that you can't rely on this "feature" of common compilers.

做强是注释5在同一个区(重点煤矿):

Stronger is note 5 in the same section (emphasis mine):

如果两个声明声明功能
  具有相同名称和
  参数类型列表(8.3.5)为
  相同的命名空间的成员或
  具有相同名称声明的对象
  相同的命名空间的是成员,
  声明给予的名称
  不同的语言之间的联系时,
  程序是非法的构造; 没有诊断
  如果出现申报要求
  在不同的翻译单元

If two declarations declare functions with the same name and parameter-type-list (8.3.5) to be members of the same namespace or declare objects with the same name to be members of the same namespace and the declarations give the names different language linkages, the program is ill-formed; no diagnostic is required if the declarations appear in different translation units.

所以,我要说的是,你根本不能保证按照标准工作,编译器不需要打印诊断为你所赐给因为的声明是在不同的翻译单元的例子

So, I would say that what you did is not guaranteed to work according to the standard, and the compiler is not required to print a diagnostic for the example you have given because the declarations are in different translation units.

仅供参考,它为我工作与雪豹gcc和g ++ 4.2.1版。

FYI, it "works for me" with gcc and g++ version 4.2.1 on Snow Leopard.

这篇关于C ++引用的参数和C联动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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