Topic: How to overload ostream's << operator?


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/11/19
Raw View
Eberhard Schweizer wrote:

> The problem comes with double and float values: The rounding
> algorithm to output those values as ascii text is different on various
> platforms. As a consequence this leads to different output.

Anyway if the internal representation of the value isn't the
same, you'll have this problem with the same algorithm.

> Remembering of C++'s overloading feature (and the fact, that ostream is
> a base class of all the others) I tried the following:
>
>    ostream& operator << (ostream &os, double x)
>    {... return os ; }
>

> For example
>   double x ;
>   ...
>   cout << x ;
>
> Is there a way to avoid the ambiguity and to force the compilers using
> my own operator<<(double) method?

No way. You could define a class spec_rounding:

class spec_rounding {
    friend ostream& operator<< (ostream&, spec_rounding);
    double d;
public:
    spec_rounding {double);
};

cout << spec_rounding (pi);

Or define a locale, then 'imbue' it. You'll have to find
an iostream library supporting locales, but that's an
elegant way to do it.

For more info about locales, read Nathan Myers' article:
http://www.cantrip.org/locale.html

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: ebschwei@si9865.si.bosch.de (Eberhard Schweizer)
Date: 1997/11/19
Raw View
Hi,

using the operator << with stream classes ostream, ofstream, ostrstream,
ostream_withassign: How can I yield exactly the same output running a
c++-program on different platforms?

The problem comes with double and float values: The rounding
algorithm to output those values as ascii text is different on various
platforms. As a consequence this leads to different output.

Remembering of C++'s overloading feature (and the fact, that ostream is
a base class of all the others) I tried the following:

   ostream& operator << (ostream &os, double x)
   {... return os ; }


This does fine as long as I use the CC C++-compiler under solaris.
But as soon as I change the compiler (e.g. GNU-C++) the compilation
results in various ambiguity errors, i.e. the compiler does not know,
which of the 'operator<<()' methods or functions to use, whenever I
use the operator << in order to output a double value. For example
  double x ;
  ...
  cout << x ;

Is there a way to avoid the ambiguity and to force the compilers using
my own operator<<(double) method?
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]