本文介绍了传递一个C ++一系列复杂到C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要复杂的数据传递到从C C函数++。在C ++中的数据自然地存储在一个
的std ::矢量<的std ::复杂的> ç。 C函数预期数据的双阵列,双A [] 使得[0] =回复(C [0]),一个[1] =即时通讯(C [0]),一个[2] =重(三[1]),等。

I must pass complex data to a C function from C++. In C++ the data is naturally stored in astd::vector<std::complex> c. The C function expects the data as an array of double, double a[] such that a[0]=Re(c[0]), a[1]=Im(c[0]), a[2]=Re(c[1]), etc.

什么是通过这样的数据的最佳安全的方式?是铸造像

What is the best safe way to pass such data? Is casting like

(double*) (&c[0])

在自找麻烦?

很抱歉,如果这是重复的,我只能找到通过C ++复杂C99复杂的相关的问题的信息。

Sorry if this is duplicate, I could only find information on the related problem of passing C++ complex to C99 complex.

推荐答案

在的C ++ 0x标准去长度,以保证这种转换就可以了(§26.4):

The C++0x standard went to lengths to guarantee that such conversions will work (§26.4):

此外,如果是类型 CV的std ::复杂&LT的前pression; T&GT; * 和前pression 一[I] 为一个整数前pression 我,则:

- reinter pret_cast&LT; CV T *&GT;(一)[2 * I] 应指定的实部A [我]

- reinter pret_cast&LT; CV T *&GT;(一)[2 * I + 1] 应指定的虚部一个[I]

和(§23.3.6):

and (§23.3.6):

向量的元素被连续存储,这意味着如果 v 矢量&lt; T,分配器&GT; 其中, T 是某种类型不是布尔等,那么它服从的身份&放大器; v [N] ==&放大器; v [0] + N 所有 0℃; = N&LT; v.size()

复数值的布局的的由prevailing保证C ++标准03(尽管矢量布局),但我会惊讶地发现了其中实现它不成立。

The layout of the complex value is not guaranteed by the prevailing C++03 standard (though the vector layout is), but I would be surprised to find an implementation for which it does not hold.

这篇关于传递一个C ++一系列复杂到C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 13:19
查看更多