C99 _Complex and C++ complex<> interoperability.
Ed Smith-Rowland
3dw4rd@verizon.net
Fri Feb 23 04:05:00 GMT 2007
Howard Hinnant wrote:
> On Feb 22, 2007, at 1:25 PM, Smith-Rowland, Edward M wrote:
>
>> My questions are:
>> Is this behavior standard or is this a gcc extension?
>> If this is a gcc extension where is it documented?
>>
>> Also, if this behavior is nonstandard is anyone thinking about
>> standardizing it?
>> Finally, the circle would be complete if we could assign from
>> std::complex to _Complex.
>> Any ideas? There is a private functions that return _Complex from
>> std::complex.
>
> Fwiw here are some relevant links:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#387
>
> The above link is a C++ committee library working group defect report
> on this subject. It is in an open state meaning that no decision has
> been made.
>
> Below is a link to a paper which further discusses (really just
> summarizes) the issue:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1589.html
>
> In a nutshell, there is support on the C++ committee for making the
> C++ and C99 complex types layout compatible. I'm not aware of support
> for making them API compatible. Currently C++ will not parse the
> syntax for C99 complex types and I know of know proposal to do so.
Actually, g++ happily accepts a mixture of things like
"std::complex<float>" and "double _Complex" as the code attached to the
first mail demonstrates. I will attempt to see if this compiles on
Visual Studio 2005.
It actually doesn't surprise me that these types could appear side by
side - "double _Complex" is a built in type like double as far as C99 is
concerned and of course std::complex<> is just another type. I think
that for C and C++ compilers that share a common codebase this is not a
stretch in practice.
I was a little more surprised that C++ complex vars could take
assignments from C99 complex and the C++ ctors could take C99 input.
I guess I'll come out and say it: I think the feature of having C++
standard complex variables accept input from C99 complex numbers is a
good idea in the interest of interoperability. Getting the C++ powers
to accept _Complex as a new keyword might be a bit of a challenge though
;-). But it would make using C libraries within C++ easier.
If this was an accident or an experiment in gcc that will never be
standard I still think we should put it up as an extension in the docs.
> However if the complex types were layout compatible one could link
> C99 and C++ sources and have them refer to the very same complex data
> structures, even passing them back and forth (just referenced with
> different syntax in the different translation units).
I really also would add a strong vote to mandate the complex layout -
contiguous real complex - as standard.
I think I speak for at least some people in the GSL and other scientific
communities too.
> -Howard
>
As long as I'm voicing my opinion on such matters (FWIW) I would go for
std::complex<T>::set_real(T) and std::complex<T>::set_imag(T) if only
lvalue functions are a tad untraditional in C++.
Ed
More information about the Libstdc++
mailing list