Does the Unary + Operator Have Any Practical Use

Does the unary + operator have any practical use?

char ch = 'a';
std::cout << ch << '\n';
std::cout << +ch << '\n';

The first insertion writes the character a to cout. The second insertion writes the numeric value of ch to cout. But that's a bit obscure; it relies on the compiler applying integral promotions for the + operator.

What's the significant use of unary plus and minus operators?

The Unary + operator converts its operand to Number type.
The Unary - operator converts its operand to Number type, and then negates it.
(per the ECMAScript spec)

In practice, Unary - is used for simply putting negative numbers in normal expressions, e.g.:

var x = y * -2.0;

That's the unary minus operator at work. The Unary + is equivalent to the Number() constructor called as a function, as implied by the spec.

I can only speculate on the history, but the unary +/- operators behave similarly in many C-derived languages. I suspect the Number() behavior is the addition to the language here.

What is the purpose of the unary plus (+) operator in C?

As per the C90 standard in 6.3.3.3:

The result of the unary + operator is the value of its operand. The integral promotion is
performed on the operand. and the result has the promoted type.

and

The operand of the unary + or - operator shall have arithmetic type..

What's the purpose of the + (pos) unary operator in Python?

I believe that Python operators where inspired by C, where the + operator was introduced for symmetry (and also some useful hacks, see comments).

In weakly typed languages such as PHP or Javascript, + tells the runtime to coerce the value of the variable into a number. For example, in Javascript:

   +"2" + 1
=> 3
"2" + 1
=> '21'

Python is strongly typed, so strings don't work as numbers, and, as such, don't implement an unary plus operator.

It is certainly possible to implement an object for which +obj != obj :

>>> class Foo(object):
... def __pos__(self):
... return "bar"
...
>>> +Foo()
'bar'
>>> obj = Foo()
>>> +"a"

As for an example for which it actually makes sense, check out the
surreal numbers. They are a superset of the reals which includes
infinitesimal values (+ epsilon, - epsilon), where epsilon is
a positive value which is smaller than any other positive number, but
greater than 0; and infinite ones (+ infinity, - infinity).

You could define epsilon = +0, and -epsilon = -0.

While 1/0 is still undefined, 1/epsilon = 1/+0 is +infinity, and 1/-epsilon = -infinity. It is
nothing more than taking limits of 1/x as x aproaches 0 from the right (+) or from the left (-).

As 0 and +0 behave differently, it makes sense that 0 != +0.

What is the purpose of a unary + before a call to std::numeric_limitsunsigned char members?

The output operator << when being passed a char (signed or unsigned) will write it as a character.

Those function will return values of type unsigned char. And as noted above that will print the characters those values represent in the current encoding, not their integer values.

The + operator converts the unsigned char returned by those functions to an int through integer promotion. Which means the integer values will be printed instead.

An expression like +std::numeric_limits<unsigned char>::lowest() is essentially equal to static_cast<int>(std::numeric_limits<unsigned char>::lowest()).

Why would one use the unary operator on a property in ruby? i.e &:first

Read the answers in the duplicate questions for the meaning and usage of &:.... In this case, entries is an array, and there are three methods map, sort_by, and map chained. sort_by(&:last) is equivalent to sort_by{|x| x.last}. map(&:first) is the same as map{|x| x.first}. The reason the first map does not use &:... is because (i) the receiver of accept_entry is not e, and (ii) it takes an argument e.

Common Practice for Unary Operators

PEP8 describes that higher precedence operators should have no separation between them ie:

a = -a
a = +a

and other operators like these should have this spacing.

What does the unary plus operator do?

It's there to be overloaded if you feel the need; for all predefined types it's essentially a no-op.

The practical uses of a no-op unary arithmetic operator are pretty limited, and tend to relate to the consequences of using a value in an arithmetic expression, rather than the operator itself. For example, it can be used to force widening from smaller integral types to int, or ensure that an expression's result is treated as an rvalue and therefore not compatible with a non-const reference parameter. I submit, however, that these uses are better suited to code golf than readability. :-)

Why the unary * operator does not have a constraint the operand shall not be a pointer to void?

One possible (though somewhat contrived, I'll admit) case where adding your 'suggested' constraint would break code is where the & and * operators are concatenated. In such cases, an expression such as a = &*p, where p is a void* type, is allowed.

From this Draft Standard, immediately following the section in your first citation (bold emphasis mine):

Semantics

3     The unary & operator yields the address of its
operand. If the operand has type ‘‘type’’, the result has type
‘‘pointer to type’’. If the operand is the result of a unary *
operator, neither that operator nor the & operator is evaluated

and the result is as if both were omitted, except that the
constraints on the operators still apply
and the result is not an
lvalue. …

I can't, currently, think of a use-case for that &* combination (on a void* or any other pointer type) – but it may occur in code that is "auto-generated" and/or uses conditional macro expansion(s).



Related Topics



Leave a reply



Submit