Andrei Alexandrescu wrote:
Walter Bright wrote:
Andrei Alexandrescu wrote:
Walter Bright wrote:
Andrei Alexandrescu wrote:
Is there any good use of unary +? As an aside, Perl programs do use
it occasionally for syntactic disambiguation :o).
An internet search reveals:
1. symmetry
2. compatibility with C and many other languages that use it
3. used with operator overloading to convert a user defined type to
its preferred arithmetic representation (a cast can't know what the
'preferred' type is)
4. to create DSL languages, like Spirit, as Kenny points out
5. to coerce default integral promotion rules (again, cast(int)
won't always produce the same result)
6. to visually emphasize that a literal is positive
I say leave it in.
I am completely underwhelmed by 1-6 and have strong arguments against
each, but "frankly, my dear" I have bigger problems than that. I have
exactly zero valid reasons I could mention in TDPL, and that's my
litmus test. I find the operator utterly useless. If '+' stays in,
then call it horsetrading but the occasionally useful '^^=' must also
be in.
Think of it like the "bool" operator overload. bool gives a direct way
for user defined times to be tested for if statements, etc.
If I think of it that way, that doesn't look too good. The bool operator
overload has been an unqualified and admitted failure for C++. The hacks
used to avoid that failure are very ingenious; none was within the realm
of what the language had planned or intended.
Similarly, U+ gives a direct way for user defined types to be
converted to their most desired arithmetic type.
Code that uses +a to convert a to another type? I'd consider that worse
than a hack and worse than incompetent - it's downright malicious.
Andrei
And if Andrei has never heard of it, we can be pretty sure that very few
programmmers would understand code that uses it.
It's really looking as though U+ is only for literals and for operator
abuse.