| [1966] | 1 | #include <stdlib.h> | 
|---|
|  | 2 | #include <stdio.h> | 
|---|
|  | 3 |  | 
|---|
|  | 4 |  | 
|---|
|  | 5 | #include <iostream.h> | 
|---|
|  | 6 | #include <complex> | 
|---|
|  | 7 |  | 
|---|
|  | 8 | // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | 
|---|
|  | 9 | // Test program illustrating g++ problems with conversion | 
|---|
|  | 10 | // operators involving the complex<T> class | 
|---|
|  | 11 | // Also, the complex class lacks the ostream& << operator | 
|---|
|  | 12 | //         R. Ansari (LAL/IN2P3-CNRS) - April 2002 | 
|---|
|  | 13 | // # The flag CONV_DOSZ activates the ostream& << operator | 
|---|
|  | 14 | // # definition in the source code | 
|---|
|  | 15 | // # To test compilation with implicit conversion operator | 
|---|
|  | 16 | // csh> g++ -DCONV_ICO -DCONV_DOSZ -o pbconv pbconv.cc | 
|---|
|  | 17 | // # To test compilation with explicit conversion operator | 
|---|
|  | 18 | // csh> g++ -DCONV_ECO -DCONV_DOSZ -o pbconv pbconv.cc | 
|---|
|  | 19 | // # To test compilation with static_cast conversion operator | 
|---|
|  | 20 | // csh> g++ -DCONV_STCAST -DCONV_DOSZ -o pbconv pbconv.cc | 
|---|
|  | 21 | // # To run the produced test programs | 
|---|
|  | 22 | // csh> ./pbconv | 
|---|
|  | 23 | // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | 
|---|
|  | 24 |  | 
|---|
|  | 25 | using namespace std; | 
|---|
|  | 26 |  | 
|---|
|  | 27 | #if defined(CONV_DOSZ) | 
|---|
|  | 28 | // the complex class in libstdc/g++ lacks the << operator definition ! | 
|---|
|  | 29 | template <class T> | 
|---|
|  | 30 | inline ostream& operator << (ostream& os, const complex<T>& z) | 
|---|
|  | 31 | { os << z.real() << "," << z.imag() << " " ;  return(os); } | 
|---|
|  | 32 | #endif | 
|---|
|  | 33 | // -------------------------------------------------------------------- | 
|---|
|  | 34 | // A class with a conversion operator to complex<double> | 
|---|
|  | 35 |  | 
|---|
|  | 36 | class convCR { | 
|---|
|  | 37 | public: | 
|---|
|  | 38 | inline convCR() { dv = 0.;  dv_im = 0.; } | 
|---|
|  | 39 | inline convCR(convCR const& x) { dv = x.dv;  dv_im = x.dv_im; } | 
|---|
|  | 40 | inline convCR(double v) { dv = v;  dv_im = 0.; } | 
|---|
|  | 41 | inline convCR(complex<double> const & z) { dv = z.real();  dv_im = z.imag(); } | 
|---|
|  | 42 |  | 
|---|
|  | 43 | inline convCR&  operator= (double v) { dv = v;  dv_im = 0.; return (*this); } | 
|---|
|  | 44 | inline  convCR& operator= (complex<double> const & z) | 
|---|
|  | 45 | { dv = z.real(); dv_im = z.imag(); return (*this); } | 
|---|
|  | 46 |  | 
|---|
|  | 47 | inline operator double() const  { return(dv); } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | inline operator complex<double>() const { return(complex<double>(dv, dv_im)); } | 
|---|
|  | 50 | inline complex<double> ToComplex() const { return(complex<double>(dv, dv_im)); } | 
|---|
|  | 51 | double dv; | 
|---|
|  | 52 | double dv_im;   /* for holding imaginary part of a complex */ | 
|---|
|  | 53 | }; | 
|---|
|  | 54 | // -------------------------------------------------------------------- | 
|---|
|  | 55 |  | 
|---|
|  | 56 | // ------------  The main program ----------------- | 
|---|
|  | 57 | int main(int narg, char *arg[]) | 
|---|
|  | 58 | { | 
|---|
|  | 59 | int rc = 0; | 
|---|
|  | 60 | try { | 
|---|
|  | 61 | cout << " ---->  complex numbers and convCR : " << endl; | 
|---|
|  | 62 | complex<double> z,z1(2.5,2.5),z2(3.3,-1); | 
|---|
|  | 63 | cout << " complex<double>: z1=" << z1 << " z2=" << z2 | 
|---|
|  | 64 | << " z1+z2=" << z1+z2 << endl; | 
|---|
|  | 65 | convCR czr, cz1(z1), cz2(z2), cz3; | 
|---|
|  | 66 | /* if defined(__GNUG__) && (__GNUC__ < 3) */ | 
|---|
|  | 67 | complex<double> zb,zc; | 
|---|
|  | 68 | cout << " Using convCR::ToComplex() method : " << endl; | 
|---|
|  | 69 | zb = cz1.ToComplex(); zc = cz2.ToComplex(); | 
|---|
|  | 70 | cout << " convCR:.ToComplex() zb=czr1= " << zb << " zc=" << zc | 
|---|
|  | 71 | << " zb+zc=" << zb+zc << endl ; | 
|---|
|  | 72 | #if defined(__GNUG__) | 
|---|
|  | 73 | cout << " GNUG_Version: __GNUC__ = " << __GNUC__ << endl; | 
|---|
|  | 74 | // else | 
|---|
|  | 75 | #endif | 
|---|
|  | 76 |  | 
|---|
|  | 77 | // Implicit conversion operator - | 
|---|
|  | 78 | // compiles OK with g++ 2.95, not with g++ 3.01 | 
|---|
|  | 79 | #if defined(CONV_ICO) | 
|---|
|  | 80 | complex<double> za; | 
|---|
|  | 81 | za = cz1; | 
|---|
|  | 82 | cout << " complex<double> za; za = cz1 : " << za << endl; | 
|---|
|  | 83 | #endif | 
|---|
|  | 84 |  | 
|---|
|  | 85 | #if defined(CONV_ECO) | 
|---|
|  | 86 | // Explicit conversion operator, does not compile with gcc | 
|---|
|  | 87 | //  (g++ 2.95 , 3.01 ) | 
|---|
|  | 88 | cout << " Using conversion operator on convCR : " << endl; | 
|---|
|  | 89 | cout << " convCR: (complex<double>)(cz1(z1))" | 
|---|
|  | 90 | << (complex<double>)(cz1) | 
|---|
|  | 91 | << " \n (complex<double>)cz2(z2)" | 
|---|
|  | 92 | << (complex<double>)(cz2) | 
|---|
|  | 93 | << endl; | 
|---|
|  | 94 | #endif | 
|---|
|  | 95 |  | 
|---|
|  | 96 | #if defined(CONV_STCAST) | 
|---|
|  | 97 | // static_cast conversion operator, does not compile with | 
|---|
|  | 98 | //  (g++ 2.95 , 3.01 ) | 
|---|
|  | 99 | complex<double> zdd; | 
|---|
|  | 100 | cout << " Using static_cast operator on convCR : " << endl; | 
|---|
|  | 101 | zdd = static_cast< complex<double> >(cz1); | 
|---|
|  | 102 | cout << " zdd = static_cast< complex<double> >(cz1) = " << zdd << endl; | 
|---|
|  | 103 | #endif | 
|---|
|  | 104 | } | 
|---|
|  | 105 | catch (...) { | 
|---|
|  | 106 | cerr << " Catched Unknown exception ! " << endl; | 
|---|
|  | 107 | rc = 99; | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | exit(rc); | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 |  | 
|---|
|  | 114 |  | 
|---|
|  | 115 |  | 
|---|
|  | 116 |  | 
|---|
|  | 117 |  | 
|---|
|  | 118 |  | 
|---|
|  | 119 |  | 
|---|