Re: perl-like string concatenation
On Mar 9, 2:45 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* James Kanze:
[...]
int i = 22;
string newString = string("Hello") + i + "whatever";
And what is that supposed to mean? Who knows what concatenating
an int to a string should mean: how many characters, what base,
etc.
Assuming for the sake of discussion that that is a problem,
then isn't that a problem also with e.g.
std::ostringstream::operator<<?
Except that the role of ostream *is* to format, and it provides
everything you need to specify exactly what you want.
I don't think such reasonable defaults are problematic.
It depends. In the case of int, maybe, maybe not. In the case
of unsigned, probably not (I usually want hex). And in the case
of floating point, almost never.
On the contrary, having to specify every little customizable
detail can IMHO be very counter-productive,
That's what formatting is all about: specifying every little
detail. Normally, you'll use custom manipulators for this, and
probably output almost nothing except label strings without a
custom manipulator before it, to indicate the semantics of the
value which follows. (How the value is formatted depends on the
semantics it has in the program.)
but it's my impression that it's almost impossible to convince
the IBM-oriented (e.g. Lotus Notes interface) and/or
math-oriented (e.g. C++0x random generators) and/or
whatever-oriented guys that such extreme verbosity is in
conflict with goals like clarity, conciseness & correctness.
And in the other direction, it's almost impossible to convince
some people that having all kinds of implicit conversion (e.g.
to char const* or to bool) is ungood, that explicit code is
better. But regarding that, a '+' is explicit.
But the conversion of int to string isn't.
Not that I think operator+ is good choice for building up
strings, because its natural semantics -- not the syntax --
are IMO unsuitable for this.
Yes. I may be na=EFve, but I'd normally expect any overloaded
operator+ to be commutative. On the other hand, there's an
enormous history in other langauges (well, at least Basic), and
it seems to be an established convention that + means
concatenate when applied to strings.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34