469,357 Members | 1,542 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,357 developers. It's quick & easy.

portable complex numbers between C, C++

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?

Oct 20 '05 #1
6 2392
In article <11**********************@g47g2000cwa.googlegroups .com>,
<ma**@borgerding.net> wrote:
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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 20 '05 #2
ma**@borgerding.net wrote:
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
Oct 20 '05 #3
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>;

Oct 20 '05 #4
<ma**@borgerding.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Oct 20 '05 #5
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:HG*******************@newsread1.mlpsca01.us.t o.verio.net...
ma**@borgerding.net wrote:
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.


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
Oct 20 '05 #6
In article <PK******************************@giganews.com>,
"P.J. Plauger" <pj*@dinkumware.com> wrote:
<ma**@borgerding.net> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.


Actually we haven't yet:

http://www.open-std.org/jtc1/sc22/wg...ctive.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
Oct 25 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Blair | last post: by
6 posts views Thread by gc | last post: by
1 post views Thread by seia0106 | last post: by
131 posts views Thread by pemo | last post: by
3 posts views Thread by Russ | last post: by
25 posts views Thread by jacob navia | last post: by
9 posts views Thread by void main | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.