C ++引用参数和C链接 [英] C++ by-reference argument and C linkage

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

问题描述

我遇到了一个工作(使用XLC8和MSFT9编译器)的代码片段,包含一个C语言文件,该函数使用C链接和引用参数定义。这让我很烦,因为引用只是C ++。该函数从C代码中调用,它被声明为用相同类型的指针参数代替引用参数。



简化示例::




$ b

C $ c> externCvoid f(int& i)
{
i ++;
}

C文件

  void f(int *); 

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

现在, C ++编译器,在引擎盖下,像指针一样实现引用。是这样,只是纯运气这个代码工作原因或它说的在C ++规范中的什么结果是什么,当你定义一个函数与参考参数和C链接?

解决方案

我的n3000.pdf副本(从此处),您可以在第7.5节中说出这一点— 链接规范


9 。从C ++到
其他语言中定义的对象和从其他语言定义的C ++中定义的对象的链接是
实现定义的和
语言相关的。只有在两个
语言实现的
对象布局策略类似
的情况下,才能实现这种链接。


由于C和C ++是不同的语言,这意味着你不能依赖普通编译器的这个特性。




如果两个声明声明具有相同名称的函数

parameter-type -list(8.3.5)为
同一命名空间的成员或
声明与
同名的对象为同一命名空间的成员和
声明给出名称
不同的语言联系,
程序不健全; 如果声明在不同的翻译单位中出现
,则不需要诊断


所以,我会说,你所做的是不能保证按照标准工作,并且编译器不需要打印一个诊断为你给出的例子,因为声明是在不同的翻译单位。



FYI,它适用于在雪豹上使用gcc和g ++版本4.2.1。


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.

Simplified example:

C++ file:

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

C file:

void f(int *);

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

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.

解决方案

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

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.

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

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

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.

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

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

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