portable complex numbers between C, C++

M

mark

I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif


How portable is this?
 
G

Greg Comeau

I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif


How portable is this?

The porability of the raw code above is not problem
in and of itself, however, it does imply a C99 implemenation
and/or compatibility across complex's which is not necessarily
there. I'm not sure if that's what you're getting at or not.
 
V

Victor Bazarov

I've got a C library that I want to access from C++. I'd like to pass
complex number portably between them. Right now, I've got code in the
header file to define the argument according to C or C++.

#ifdef __cplusplus
# include <complex>
typedef std::complex<float> float_complex;
#else
# include <complex.h>
typedef float complex float_complex;
#endif


How portable is this?

Not portable at all. If you want to pass any information between C and
C++, the data structure has to be the same, it has to be 'struct' and you
need to include the same definition in all modules. There is no "float
complex" type in C++, and therefore its internals or its behaviour is
unknown. If somehow you discover that std::complex<float> behaves the
same as "float complex" with your C++ and C compilers, its a pure
coincidence AFA languages are concerned.

V
 
M

mark

Well. I looked at the C++ standard and I found a copy of the C99
standard draft.

It looks like the C99 standard 6.1.2.5 specifies that a float complex
will have the same storage as a float[2] array with real,then imag
parts.

I am still looking through the C++ standard for storage specification
of template class complex<float>;
 
P

P.J. Plauger

Well. I looked at the C++ standard and I found a copy of the C99
standard draft.

It looks like the C99 standard 6.1.2.5 specifies that a float complex
will have the same storage as a float[2] array with real,then imag
parts.

I am still looking through the C++ standard for storage specification
of template class complex<float>;

We've added that requirement, IIRC. The data layouts should
be compatible.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
P

P.J. Plauger

Not portable at all. If you want to pass any information between C and
C++, the data structure has to be the same, it has to be 'struct' and you
need to include the same definition in all modules. There is no "float
complex" type in C++, and therefore its internals or its behaviour is
unknown. If somehow you discover that std::complex<float> behaves the
same as "float complex" with your C++ and C compilers, its a pure
coincidence AFA languages are concerned.

Not really. Both languages (now) require that the data layout be
the same as float[2], which is akin to the ancient Fortran
requirement. Note also that TR1 reconciles the C99 and C++
complex libraries.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
H

Howard Hinnant

said:
Well. I looked at the C++ standard and I found a copy of the C99
standard draft.

It looks like the C99 standard 6.1.2.5 specifies that a float complex
will have the same storage as a float[2] array with real,then imag
parts.

I am still looking through the C++ standard for storage specification
of template class complex<float>;

We've added that requirement, IIRC. The data layouts should
be compatible.[/QUOTE]

Actually we haven't yet:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#387

still Open <sigh>. But P.J.'s words are close enough. The data layout
compatibility with C99 has strong support, and if nothing else from 387
passes, that part still will (imho). Plus I know of no C++
implementations where the layout compatibility is not already there.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top