Re: perl-like string concatenation
* James Kanze:
On Mar 9, 11:50 am, Christof Warlich <cwarl...@gmx.de> wrote:
James Kanze schrieb:
On Mar 9, 8:56 am, Christof Warlich <cwarl...@gmx.de> wrote:
is there any danger in overloading operator+ as follows:
template<typename T> string operator+(const string &x, T y) {
ostringstream tmp;
tmp << x << y;
return tmp.str();
}
Only that it results in unreadable and unmaintainable code,
and that it's not really very useful.
Hmm... - why unreadable code? I'm writing a text processing
application where I quite frequently need to concatenate
strings from other strings, integers, characters, C character
strings, ..., so I found it much more readable to write
something like:
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<<?
I don't think such reasonable defaults are problematic.
On the contrary, having to specify every little customizable detail can IMHO be
very counter-productive, 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.
Not that I think operator+ is good choice for building up strings, because its
natural semantics -- not the syntax -- are IMO unsuitable for this.
I've commented on that separate issue else-thread.
Cheers,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!