问题描述
第三方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:
并根据C ++ 11:
and according to C++11:
-
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 ofa[i]
, andreinterpret_cast<cv T*>(a)[2*i + 1]
shall designate the imaginary part ofa[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复杂参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!