From 3746974797195469870
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,391f543b722aeabb,start
X-Google-Attributes: gidf78e5,public
From: Valentin Bonnard <bonnardv@pratique.fr>
Subject: Re: How to overload ostream's << operator?
Date: 1997/11/19
Message-ID: <34731B4F.2A91@pratique.fr>#1/1
X-Deja-AN: 290781881
References: <64up26$uf@sioux.fr.internet.bosch.de>
X-Original-Date: Wed, 19 Nov 1997 18:01:03 +0100
Organization: Ecole Normale Superieure, Paris, France
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANHMnteEDnX0m9pzZAQHDIAGAgM9cU4ZmAeCF3tJ1K847Ts7ZUXyOZ7TN E6EYVwmvEL+zVwfEV3sFT6czYNZF1Oc+ =cO4U
Newsgroups: comp.std.c++


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                             ]



