🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2075

Re: Integral promotion

#2075
From: Steve Clamage
Date: Fri, 29 Sep 1995 15:21
50 lines
2804 bytes
In article 95Sep29001439@Austern, MaxPolk@megaweb.com (Max Polk) writes:
>
>Note the special treatment given to char and short:
>
>char c;        short s;      int i;        long l;
>c = 'A';       s = 5;        i = 7;        l = 8L;
>c = c + 'B'    s = s + 2;    i = i + 2;    l = l + 2
>ILLEGAL        ILLEGAL       OKAY          OKAY
>
>Because of the "+" operator, we get integral promotion of 
>c=c+'B' and s=s+2 to int on both sides of the "+" and 
>following the addition we have truncation when assigning the 
>int back to the char or short.
>
>Why not allow a char plus a char equal a char, or a short plus 
>a short equal a short, just like an int plus an int DOES equal 
>and int and a long plus a long DOES equal a long.
>
>To avoid warnings, you must use a cast:
>c = (char)(c + 'B');        s = (short)(s + 2);
>which is absurdly stupid and unnecessary.  Why arbitrarily and 
>artificially force integral promotion of a char or short to an 
>int prior to the addition?

Your complaint is more with the compiler vendor than with the
language definition.

The examples you marked "illegal" are not illegal, but are well-formed
and well-defined -- except for possible overflow, which is not particularly
relevent here. There is no language requirement that any sort of
diagnostic be issued for that code. (A compiler may emit any kind of
warnings it likes, which is a separate subject. "Warning: this identifier
looks like a misspelled Portuguese word.")

Next, the "as-if" rule allows the compiler to omit the promotion to int
when the result would be the same without doing the promotion. And in fact
some compilers do char or short arithmetic for your first two examples.

As to your comments about the C promotion rules not being needed in C++,
a goal of C++ is to be as close to C as possible, and not break C
code unnecessarily. I think it would be a bad idea for integer arithmetic
to have different rules in C++ than it does in C.
---
Steve Clamage, stephen.clamage@eng.sun.com



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <44h2pm$eie@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <MATT.95Sep29001439@Austern>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: clamage@Eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Integral promotion
Date: 29 Sep 1995 15:21:25 GMT
Organization: Sun Microsystems Inc.
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <44h2pm$eie@engnews2.Eng.Sun.COM>
References: <MATT.95Sep29001439@Austern>
Reply-To: clamage@Eng
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: taumet.eng.sun.com
Content-Length: 1999
X-Lines: 46
Originator: clamage@taumet