Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What does the unary plus operator do?

operator Plus unary
0
Posted

What does the unary plus operator do?

0

Unary plus was present in C, where it did absolutely nothing (much like the auto keyword). In order to not have it, Stroustrup would have had to introduce a gratuitous incompatibility with C. Once it was in C++, it was natural to allow an overload function, just like unary minus, and Stroustrup might have introduced it for that reason if it wasn’t already there. So, it means nothing. It can be used as as sort of decoration to make things look more symmetrical, using +1.5 as the opposite to -1.5 for example. In C++, it can be overloaded, but it’s going to be confusing if operator+() does anything. Remember the standard rule: when overloading arithmetic operators, do things like the ints do. If you’re looking for a reason why it’s there, find something about the early history of C. I suspect there was no good reason, as C was not really designed. Consider the useless auto keyword (presumably in contrast to static, now being recycled in C++0x), and the entry keyword, which never did anyth

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123