Topic: Is this a valid conversion operator?


Author: bglenden@colobus.cv.nrao.edu (Brian Glendenning)
Date: Wed, 15 Jun 1994 15:18:35 GMT
Raw View
      template<class t> class complex
      {
      public:
          t real, im;
          operator complex<float>() const;
      };

      template<class t> complex<t>::operator complex<float>() const
      {
          complex<float> cf;
          cf.real = float(real);
          cf.im = float(im);
          return cf;
      }

      main()
      {
          complex<int> ci;
          complex<float> cf;  // It works if this line is commented out
          return 0;
      }

CFront compiles this, another compiler doesn't, with the message:

"test1.cc", line 8: Error: Cannot define a conversion from complex<float> to complex<float>.

As far as I can tell, the ARM doesn't disallow this, but I might be
missing something. Who's right?

[I realize that templated member functions will solve the problem a
better way, but we don't have 'em yet].

Brian
--
       Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu          Charlottesville Va.           (804) 296-0286




Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 15 Jun 1994 20:30:03 GMT
Raw View
>>>>> Brian Glendenning <bglenden@colobus.cv.nrao.edu> writes:

> "test1.cc", line 8: Error: Cannot define a conversion from complex<float> to complex<float>.

> As far as I can tell, the ARM doesn't disallow this, but I might be
> missing something. Who's right?

As far as I can tell, the WP doesn't disallow that, but it also will never
call the conversion operator automatically:

     A conversion operator is never used  to  |
  convert  a  (possibly qualified) object (or reference to an object) to  |
  the (possibly qualified) same object type (or a reference to  it),  or  |
  to  a  (possibly qualified) base class of that type (or a reference to  |
  it).

Jason




Author: ball@cygany.Eng.Sun.COM (Mike Ball)
Date: 15 Jun 1994 23:14:24 GMT
Raw View
In article 94Jun15133003@deneb.cygnus.com, jason@cygnus.com (Jason Merrill) writes:
> >>>>> Brian Glendenning <bglenden@colobus.cv.nrao.edu> writes:
>
> > "test1.cc", line 8: Error: Cannot define a conversion from complex<float> to complex<float>.

> As far as I can tell, the WP doesn't disallow that, but it also will never
> call the conversion operator automatically:

The workingpaper went through a phase when it did disallow it, and then relented
because some problems were pointed out with that (virtual conversions, for
example).  You probably need a later version of the compiler.

-Mike Ball-
SunSoft