Bug 21057 - iso C99 complex double: problems with g++
Summary: iso C99 complex double: problems with g++
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.3.4
: P2 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-04-16 12:35 UTC by Timo Hartmann
Modified: 2016-04-26 13:42 UTC (History)
1 user (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 4.5.0
Known to fail: 2.95.3, 3.2.3, 3.3.1, 3.3.4, 3.4.3, 4.0.0
Last reconfirmed: 2005-05-02 19:02:48


Attachments
stack misalignment using complex<double> (266 bytes, text/plain)
2016-04-26 13:42 UTC, Graham Frye
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Timo Hartmann 2005-04-16 12:35:16 UTC
Hallo.
I want to use the ISO C99 complex datatypes in c++ because they are faster than
the template complex types from std++.

g++ fails to work with ISO C99 complex in the following way:

typedefs+built-in-functions(conj)+unary minus are not working well together.

>>>>cat test.cpp
#include </usr/include/complex.h>

typedef complex double zcomplex;

int main()
{
  zcomplex f=1+_Complex_I*1;

  f=-_Complex_I;

  f=conj(f);

  return 0;
}

>>>>g++ -Wall test.cpp
test.cpp: In function `int main()':
test.cpp:9: error: wrong type argument to unary minus
test.cpp:11: error: wrong type argument to conjugation

>>>>g++ -v
Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.4/specs
Configured with: ../configure --enable-threads=posix --prefix=/usr
--with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man
--enable-languages=c,c++,f77,objc,java,ada --disable-checking --libdir=/usr/lib
--enable-libgcj --with-gxx-include-dir=/usr/include/g++ --with-slibdir=/lib
--with-system-zlib --enable-shared --enable-__cxa_atexit i586-suse-linux
Thread model: posix
gcc version 3.3.4 (pre 3.3.5 20040809)

gcc compiles the c-version without problems.
g++-versions 3.3.4,3.4.3(mingw),3.1.x,... are broken.
Comment 1 Andrew Pinski 2005-04-16 13:15:37 UTC
"I want to use the ISO C99 complex datatypes in c++ because they are faster than the template 
complex types from std++."

Why do you say that?  Well maybe before 4.0.0 it was slower but only slightly.
Comment 2 Wolfgang Bangerth 2005-05-02 19:02:47 UTC
Confirmed. However, this has really low priority: the C++ standard is not 
a superset of the C99 standard, so a number of things new to C99 are not 
part of C++, and this is one of them. If you want to write portable code, 
you should use the std::complex class. If you have cases where you can show 
that std::complex is slower than using the C99 means, we'd be happy to 
accept bug reports. 
 
Thanks 
  Wolfgang 
Comment 3 Dr. David Kirkby 2010-01-16 14:29:39 UTC
(In reply to comment #2)
> Confirmed. However, this has really low priority: the C++ standard is not 
> a superset of the C99 standard, so a number of things new to C99 are not 
> part of C++, and this is one of them. If you want to write portable code, 
> you should use the std::complex class. If you have cases where you can show 
> that std::complex is slower than using the C99 means, we'd be happy to 
> accept bug reports. 
>  
> Thanks 
>   Wolfgang 

Note this bug, which you considered as low priority since it applied to using C code with the C++ compiler, is not limited to the C++ compiler. Take a look at #42753 where you will see this bug stops one building C code if using gcc. 
Comment 4 Paolo Carlini 2011-09-26 15:12:58 UTC
FWIW, this has been fixed for 4.5.x.
Comment 5 Graham Frye 2016-04-26 13:42:01 UTC
Created attachment 38343 [details]
stack misalignment using complex<double>

#include <complex>
#include <iostream>
//changes
typedef std::complex<double> Cx;
Cx foo(int kojack)
{
	double x=3, y=kojack;
	return (x, y);
}
int main(int argc, char *argv[])
{
	Cx z1(3.0, 4.0);
	Cx z2(foo(4));
	std::cout << "z1=" << z1 << std::endl;
	std::cout << "z2=" << z2 << std::endl;
	// compiled with gcc 4.8.4 on lxle
	// g++ -o bug bug.cpp
	//output:
	//z1=(3,4)
	//z2=(4,0)
}
////////////end