 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rainer Grimm Guest
|
Posted: Wed Oct 26, 2005 10:18 am Post subject: setw für string und stringstream |
|
|
Hallo.
Folgendes Programm ergibt verschiedene Ausgaben.
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
int
main(int,char **)
{
std::stringstream stream;
std::string str("1234567890");
stream << std::setw(3) << str;
std::cout << std::setw(3) << str << "n";
std::cout << stream.str() << "n";
return 0;
}
Auf IRIX64-6.5 mit dem MIPSpro Compilers: Version 7.4:
<<<<
123
123
<<<<
Hingegen auf IRIX64-6.5 mit dem gcc version 3.3.3:
<<<<<
1234567890
1234567890
<<<<<
Ist das ein bug oder undefiniertes Verhalten.
Gruß
Rainer
--
_________________________creating IT solutions
Rainer Grimm
Entwicklung scVENUS science + computing ag
phone +49(0)7071 9457-253 Hagellocher Weg 71-75
fax +49(0)7071 9457-511 D-72070 Tuebingen, Germany
[email]r.grimm (AT) science-computing (DOT) de[/email] www.science-computing.de
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Wed Oct 26, 2005 5:33 pm Post subject: Re: setw für string und stringstream |
|
|
Rainer Grimm <r.grimm (AT) science-computing (DOT) de> writes:
| Quote: | #include <iostream
#include
#include
#include
int
main(int,char **)
{
std::stringstream stream;
std::string str("1234567890");
stream << std::setw(3) << str;
std::cout << std::setw(3) << str << "n";
std::cout << stream.str() << "n";
return 0;
}
Auf IRIX64-6.5 mit dem MIPSpro Compilers: Version 7.4:
123
123
Hingegen auf IRIX64-6.5 mit dem gcc version 3.3.3:
1234567890
1234567890
Ist das ein bug oder undefiniertes Verhalten.
|
Dein Programm hat undefiniertes oder zumindest unspezifiertes
Verhalten, aber aus einem anderen Grund:
std::cout, aber nicht alle operator<<-Überladungen, die das Programm
verwendet. Die sind in
das Programm deshalb auch #includen.
Abgesehen davon ist §21.3.7.9 klar: std::cout << std::setw(3) << str
setzt std::cout.width() auf den Wert 3, gibt dann (maximal) 3 Zeichen
aus und setzt anschliessend std::cout.width() auf 0. Die richtige
Ausgabe ist demnach:
123
123
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Thu Oct 27, 2005 9:08 am Post subject: Re: setw für string und stringstream |
|
|
Thomas Maeder wrote:
| Quote: | Abgesehen davon ist §21.3.7.9 klar: std::cout << std::setw(3) << str
setzt std::cout.width() auf den Wert 3, gibt dann (maximal) 3 Zeichen
aus und setzt anschliessend std::cout.width() auf 0.
|
Das war ein Bug in der 1998er Ausgabe des Standards, welcher 2003
korrigiert worden ist - in § 21.3.7.9/4 wurde
"[...] where n is the smaller of os.width() and str.size() [...]"
durch
"[...] where n is the larger of os.width() and str.size() [...]"
ersetzt. Das alte Verhalten lag ja auch im Widerspruch zu § 27.6.2.5.4/4.
width() führt also bei der Ausgabe nie zum Informationsverlust durch
Abschneiden.
MfG
Falk
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Thu Oct 27, 2005 5:16 pm Post subject: Re: setw für string und stringstream |
|
|
Falk Tannhäuser <falk.tannhauser (AT) crf (DOT) canon.fr> writes:
| Quote: | Thomas Maeder wrote:
Abgesehen davon ist §21.3.7.9 klar: std::cout << std::setw(3) << str
setzt std::cout.width() auf den Wert 3, gibt dann (maximal) 3 Zeichen
aus und setzt anschliessend std::cout.width() auf 0.
Das war ein Bug in der 1998er Ausgabe des Standards, welcher 2003
korrigiert worden ist - in § 21.3.7.9/4 wurde
"[...] where n is the smaller of os.width() and str.size() [...]"
durch
"[...] where n is the larger of os.width() and str.size() [...]"
ersetzt. Das alte Verhalten lag ja auch im Widerspruch zu § 27.6.2.5.4/4.
width() führt also bei der Ausgabe nie zum Informationsverlust durch
Abschneiden.
|
Aha. Danke!
Dann haben also beide Implementation irgendwie recht.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|