Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using _Complex in template

39 views
Skip to first unread message

w...@totalbb.net.tw

unread,
Jan 2, 2018, 6:19:45 AM1/2/18
to
I am implementing a Complex template using C's _Complex, like below:

#include <complex.h>

template<typename T>
class Complex {
T _Complex m_z;
public:
...
}

But g++ says:
error: complex invalid for ‘m_z’
T _Complex m_z;
^~~

How to go around this issue? Thanks.

David Brown

unread,
Jan 2, 2018, 6:50:29 AM1/2/18
to
On 02/01/18 12:19, w...@totalbb.net.tw wrote:
> I am implementing a Complex template using C's _Complex, like below:
>

C++ does not support C's _Complex types. Use C++'s complex number support.

In most cases, C is a subset of C++. But complex number support is
different.

C complex numbers:
<http://en.cppreference.com/w/c/numeric/complex>

C++ complex numbers:
<http://en.cppreference.com/w/cpp/numeric/complex>

Marcel Mueller

unread,
Jan 2, 2018, 1:30:20 PM1/2/18
to
On 02.01.18 12.19, w...@totalbb.net.tw wrote:
> I am implementing a Complex template using C's _Complex, like below:
[...]
> But g++ says:
> error: complex invalid for ‘m_z’
> T _Complex m_z;
> ^~~
>
> How to go around this issue? Thanks.

std::complex<T> m_z;

C++ does not support C's complex type, as David mentioned.
But the standard guarantees that the implementation of std::complex<T>
is binary compatible to T[2] and this is compatible to C's _Complex. So
it works even if you need to work with a C API.

Btw: what do you want to define your own complex class. Doesn't
complex<> fit your needs?


Marcel

w...@totalbb.net.tw

unread,
Jan 2, 2018, 10:10:26 PM1/2/18
to
On Wednesday, January 3, 2018 at 2:30:20 AM UTC+8, Marcel Mueller wrote:
> On 02.01.18 12.19, w...@totalbb.net.tw wrote:
> > I am implementing a Complex template using C's _Complex, like below:
> [...]
> > But g++ says:
> > error: complex invalid for ‘m_z’
> > T _Complex m_z;
> > ^~~
> >
> > How to go around this issue? Thanks.
>
> std::complex<T> m_z;
>
> C++ does not support C's complex type, as David mentioned.
> But the standard guarantees that the implementation of std::complex<T>
> is binary compatible to T[2] and this is compatible to C's _Complex. So
> it works even if you need to work with a C API.
>
My old implement is like below:

template<typename T> class MyComplex {
T m_re,m_im;
public:
...
};

It also guarantees binary compatible with std::complex<T> (so far).

Somehow, when I recently looked back at MyComplex codes, decided
to reimplement(refactor) it. The choices are 1) wrap std::complex<T>
2) wrap C's _Complex type. Choice 1) was tried... finally gave up
(difficult probably,file deleted). So what left is choice 2) wrap
C's _Complex type. Currently I use template specialization, it works.

> Btw: what do you want to define your own complex class. Doesn't
> complex<> fit your needs?
>
>
> Marcel
The requirement dates back around year 2000, when people are enthusiastic
making 1.44M bootable(Linux) floppy disk containing a demo GUI program,..
Program size was the primary concern. That program should link as little
codes as possible from c++ library. Though this restrict is relaxed, API
consistency is still a must. "Link to clib" is still a good property.

Real Troll

unread,
Jan 2, 2018, 10:57:50 PM1/2/18
to
On 03/01/2018 03:10, w...@totalbb.net.tw wrote:
> My old implement is like below:
>
> template<typename T> class MyComplex {
> T m_re,m_im;
> public:
> ...
> };
>
>


Microsoft way of doing things is like so:

|template <> class complex<double> { public: constexpr complex( double
RealVal = 0, double ImagVal = 0); constexpr complex(const
complex<double>& complexNum); constexpr explicit complex(const
complex<long double>& complexNum); // rest same as template class
complex }; |

They also give you an example:

|// complex_comp_dbl.cpp // compile with: /EHsc #include <complex>
#include <iostream> int main( ) { using namespace std; double pi =
3.14159265359; // The first constructor specifies real & imaginary parts
complex <double> c1 ( 4.0 , 5.0 ); cout << "Specifying initial real &
imaginary parts,\n" << " as type double gives c1 = " << c1 << endl; //
The second constructor initializes values of the real & // imaginary
parts using those of complex number of type float complex <float>
c2float ( 4.0 , 5.0 ); complex <double> c2double ( c2float ); cout <<
"Implicit conversion from type float to type double," << "\n gives
c2double = " << c2double << endl; // The third constructor initializes
values of the real & // imaginary parts using those of a complex number
// of type long double complex <long double> c3longdouble ( 4.0 , 5.0 );
complex <double> c3double ( c3longdouble ); cout << "Explicit conversion
from type float to type double," << "\n gives c3longdouble = " <<
c3longdouble << endl; // The modulus and argument of a complex number
can be recovered double absc3 = abs ( c3longdouble ); double argc3 = arg
( c3longdouble ); cout << "The modulus of c3 is recovered from c3 using:
abs ( c3 ) = " << absc3 << endl; cout << "Argument of c3 is recovered
from c3 using:\n arg ( c3 ) = " << argc3 << " radians, which is " <<
argc3 * 180 / pi << " degrees." << endl; } \* Output: Specifying initial
real & imaginary parts, as type double gives c1 = (4,5) Implicit
conversion from type float to type double, gives c2double = (4,5)
Explicit conversion from type float to type double, gives c3longdouble =
(4,5) The modulus of c3 is recovered from c3 using: abs ( c3 ) = 6.40312
Argument of c3 is recovered from c3 using: arg ( c3 ) = 0.896055
radians, which is 51.3402 degrees. *\ |


Real Troll

unread,
Jan 2, 2018, 11:03:42 PM1/2/18
to
On 03/01/2018 03:59, Real Troll wrote:
> Microsoft way of doing things is like so:

The formatting didn't work in the previous post so here it is again:

Microsoft way of doing things is like so:

template <>
class complex<double> {
public:
constexpr complex(
double RealVal = 0,
double ImagVal = 0);

constexpr complex(const complex<double>& complexNum);

constexpr explicit complex(const complex<long double>& complexNum);
// rest same as template class complex
};



w...@totalbb.net.tw

unread,
Jan 4, 2018, 10:17:56 PM1/4/18
to
Thanks. That's informative.
0 new messages