On Sep 25, 9:17 am, "Bill Davy" <B...@XchelSys.co.ukwrote:
Quote:
"James Kanze" <james.ka...@gmail.comwrote in message
So, I define operator<< and a template:
Quote:
template string Stringify(const T &v)
{
ostringstream;
os << v << ends;
return os.str();
}
Why a template?
Quote:
I'm not sure that's better than what I wanted to do, is it?
A reader can see what (const char*)t does and it is idiomatic.
It's certainly not idiomatic, and it's already defined for enum
types---with a meaning that certainly isn't what you want.
(It's a reinterpret_cast.) In general, implicit conversions to
char const* (or to other pointer types) are to be avoided.
Quote:
I do not want to be accused of premature optimisation, but
this solution goes a long way round the houses (oops, another
idiom) to turn 1 into "DMM".
It's not a question of optimization or whatever. The idiomatic
method of streaming data in C++ is to use operator<<, defining
operator<< for whatever types need streaming. Anything else is
obfuscation. That doesn't mean that it isn't worthwhile to
provide a general function for obtaining a symbolic value, as a
string, from an enum value---if nothing else, you could use it
to implement the operator<<. But the conversion shouldn't be
implicit. You want a named function.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34