从C ++提供C99复杂参数 [英] Supply C99 complex arguments from C++

查看:202
本文介绍了从C ++提供C99复杂参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

将C ++复杂数组传递给C

第三方C库期望将C99复数数组作为参数,从C ++调用它的最简单方法是什么,其中我的复数使用STL复杂类型?我可以把它包装在一个新的c函数,接受浮点数并将它们转换为复杂,但有更直接的方法吗?

If a third party C library expects an array of C99 complex numbers as an argument, what is the easiest way to call it from C++, where my complex numbers use the STL complex type? I could just wrap it in a new c function that accepts floats and converts them to complex, but is there a more direct way to do it?

推荐答案

根据C99:


6.2.5 / 13每个复杂类型具有与数组
类型相同的表示和对齐要求,该类型只包含相应实数类型的两个元素;第一个元素是复合
号的
等于实部,而第二个元素是虚部。

6.2.5/13 Each complex type has the same representation and alignment requirements as an array type containing exactly two elements of the corresponding real type; the first element is equal to the real part, and the second element to the imaginary part, of the complex number.

并根据C ++ 11:

and according to C++11:


26.4如果 a cv * std :: complex< T> * 类型的表达式,表达式 a [i] 为整数表达式定义 i ,则:

26.4 if a is an expression of type cv* std::complex<T>* and the expression a[i] is well-defined for an integer expression i, then:




  • reinterpret_cast< cv T *>(a)[2 * i] 应指定 a [i] code>和

  • reinterpret_cast< cv T *>(a)[2 * i + 1] a [i]的虚部

    • reinterpret_cast<cv T*>(a)[2*i] shall designate the real part of a[i], and
    • reinterpret_cast<cv T*>(a)[2*i + 1] shall designate the imaginary part of a[i]
    • 两种类型具有相同的布局,因此您可以简单地将C函数传递给 std :: complex 的数组的指针。

      Together, these mean that the two types have the same layout, so you can simply pass the C function a pointer to the array of std::complex.

      请注意,旧版本的C ++不能保证这种布局。

      Note that older versions of C++ did not guarantee this layout.

      这篇关于从C ++提供C99复杂参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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