Unary plus integral promotion

  • Thread starter bruno.van.dooren
  • Start date
B

bruno.van.dooren

According to various documentation I've read, the unary plus returns
the value of its operand after integral promotion.

Now consider the following example:
short a = 1;
short b = 2;
b = +a;

Shouldn't the assignment of +a to b generate a compiler warning that
an int is assigned to a short? Because if I understood correctly, the
result of +a should be int.

I know that
b += a;
will generate a warning because of integral promotion.
 
K

Kai-Uwe Bux

According to various documentation I've read, the unary plus returns
the value of its operand after integral promotion.
Correct.

Now consider the following example:
short a = 1;
short b = 2;
b = +a;

Shouldn't the assignment of +a to b generate a compiler warning that
an int is assigned to a short?

"Should" is a little ambiguous. As far as the standard is concerned, no
diagnostics is required. (For a different interpretation, see below.)

Because if I understood correctly, the result of +a should be int.

The result is an int, but assignment

short <- int

does not require diagnostics. It's perfectly legal.

I know that
b += a;
will generate a warning because of integral promotion.

Warnings are up to the implementor. It's purely a quality of implementation
issue. In that sense, maybe your example "should" generate a warning. If
you want to know the rationale for the behavior of the compiler you are
using, you should ask the vendor or post in a forum for that particular
implementation.


Best

Kai-Uwe Bux
 
B

Bruno

Thanks. I will do.

Btw, what's the point of the unary plus operator?
I can see a use for the unary -, but since integral pro,otion happens
automatically when it is needed, it seems redundant to me.
 
K

Kai-Uwe Bux

Bruno said:
Thanks. I will do.

Btw, what's the point of the unary plus operator?
I can see a use for the unary -, but since integral pro,otion happens
automatically when it is needed, it seems redundant to me.

One thing that comes to mind is that it renders expression like

+ some_var + some_other_var + yet_another_var

legal. This can come in handy when you want to generate code mechanically:
you do not need to treat the first summand in a special way. However, that
is just speculation. I never had a use for unary +. But, it does not hurt,
does it?


Best

Kai-Uwe Bux
 
B

Bruno

One thing that comes to mind is that it renders expression like

+ some_var + some_other_var + yet_another_var

legal. This can come in handy when you want to generate code mechanically:
you do not need to treat the first summand in a special way. However, that
is just speculation. I never had a use for unary +. But, it does not hurt,
does it?

True. this is indeed one possible use case.
And it doesn't hurt. Even if nobody would use it, it should probably
exist for completeness sake.
 
J

James Kanze

One thing that comes to mind is that it renders expression like
+ some_var + some_other_var + yet_another_var
legal. This can come in handy when you want to generate code
mechanically: you do not need to treat the first summand in a
special way. However, that is just speculation. I never had a
use for unary +. But, it does not hurt, does it?

I suspect that there's an orthogonality issue involved. It
would seem rather odd to have unary minus, but not unary plus.

In another thread, Alf used +0 to force a char to be treated as
an int---unary plus would work as well (and be even more
obfuscation), e.g.:

char c = 'a' ;
std::cout << +c << std::endl ;

will display "97" on my machine, whereas without the +, it
displays "a" (but I still prefer "(int)( c )").
 
B

Ben Pfaff

Bruno said:
Btw, what's the point of the unary plus operator?

In C, sometimes the unary plus operator is used to keep an
expression from being an lvalue. For example:

struct opaque {
int foo;
};

#define foo_accessor(OPAQUE) (+(OPAQUE)->foo)

Without the +, the foo_accessor macro could be used to set the
value of `foo', as in
foo_accessor(x) = 0;
With the +, the above will not compile (in C; I don't know C++
well enough to say).

C++ has better mechanisms for this kind of thing.
 
A

Andrew Koenig

Btw, what's the point of the unary plus operator?

The main point is to permit things like this:

int values[] = { -3, +2, -1, +4, 0, -6 };

In other words, in this example, the programmer wanted to make it clear that
someone thought about the sign for each initializer and didn't just forget.
 
V

Victor Bazarov

Andrew said:
Btw, what's the point of the unary plus operator?

The main point is to permit things like this:

int values[] = { -3, +2, -1, +4, 0, -6 };

In other words, in this example, the programmer wanted to make it
clear that someone thought about the sign for each initializer and
didn't just forget.

... or to make +++++++++++++++++++++++++++++++++a legal without the
need to count the pluses. "Just increment it many times, damn it!" <G>

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top