Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

c99 - C Complex Numbers in C++?

The following code compiles and runs just fine in C (at least according to 'gcc -std=gnu99'), but it fails to compile under C++, giving "line 5: error: cannot convert 'double' to 'double complex' in initialization". Does anybody know why?

#include "/usr/include/complex.h"
#include <stdio.h>

int main(int argc, char * argv[]) {
  double complex a = 3;  // ERROR ON THIS LINE
  printf("%lf
", creal(a));
  return 0;
}

I realize there is another way of doing complex numbers in C++, but I have to use C complex numbers in C++, because that is how the legacy code I was given does things. Thanks if you can help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A C++ compiler could choose to support the _Complex keyword as an extension (and a few do), but that isn't portable. If you want to have a portable C++ solution, you need to use the C++ std::complex templates, unfortunately.

The good news is that C++ std::complex numbers are guaranteed to be compatible with C complex numbers (in the sense that a pointer to one can always be converted to a pointer to the other, and the right thing will happen), which means that if you need to interoperate with a C library that expects C complex values, you won't have any trouble.

C11:

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:

If z is an lvalue expression of type cv std::complex<T> then:

— the expression reinterpret_cast<cv T(&)[2]>(z) shall be well-formed,

reinterpret_cast<cv T(&)[2]>(z)[0] shall designate the real part of z, and

reinterpret_cast<cv T(&)[2]>(z)[1] shall designate the imaginary part of z.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...