473,414 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Usual arithmetic conversions + integral promotion for short?

Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and
( sizeof(short) == sizeof((short)0 + (short)0) )
?

Regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #1
10 2239

"Niels Dekker (no reply address)" <un*****@this.is.invalid> wrote in message
news:40***************@this.is.invalid...
Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and
( sizeof(short) == sizeof((short)0 + (short)0) )
The second part of that question is *always* true! You're casting two int's
as shorts, and adding them together to create temporary value, which must
therefore also be a short. So, naturally it has the same size as a short,
because it *is* a short.

And since the first part is also true (or is allowed to be true, at least),
then...

Yes, it is possible. Even probable, I would venture to guess.

-Howard

?

Regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware

Jul 22 '05 #2
Niels Dekker (no reply address) wrote:
Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and
( sizeof(short) == sizeof((short)0 + (short)0) )
?


The latter is mandatory, AFAICT. The former is definitely possible.

Victor
Jul 22 '05 #3
Howard wrote in
news:gP*******************@bgtnsc04-news.ops.worldnet.att.net in
comp.lang.c++:

"Niels Dekker (no reply address)" <un*****@this.is.invalid> wrote in
message news:40***************@this.is.invalid...
Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and
( sizeof(short) == sizeof((short)0 + (short)0) )

For the OP:

The type of short + short is int (standard intergral promotion's),
So the answer is No.

The second part of that question is *always* true! You're casting two
int's as shorts, and adding them together to create temporary value,
which must therefore also be a short. So, naturally it has the same
size as a short, because it *is* a short.

#include <iostream>

void f( short )
{
std::cout << "short\n";
}

void f( int )
{
std::cout << "int\n";
}

int main()
{
short a = 1, b = 2;
f( a + b );
}

If you get "short" on your compiler ask for a refund.

[snip]

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
D'OH! :-)

Rob's right, of course. Adding two shorts results in an int, not a short.

It was probably designed that way originally to allow for possible overflow
without losing data (although technically it really only needs one extra
bit, but that's hard to accomplish in a C++ program...and I digress).

Testing this fact with the code Rob gave (and several variations of it),
demonstrated (at least in my compilers) that I was incorrect in my
assumption. (I don't have the standard here to look up the relevant
specification.)

So whenever a short is smaller than an int, then the second comparison
should be false. (That is, a short should *also* be smaller than the sum of
two shorts.) Which is, I'm guessng, what you thought in the first place,
right?

Sorry for the misinformation. (And, what compiler gives you those erroneous
results?)

-Howard

Jul 22 '05 #5
Niels Dekker (no reply address) wrote:
Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and
( sizeof(short) == sizeof((short)0 + (short)0) )
?


No.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6
Rob Williscroft wrote:

The type of short + short is int (standard intergral promotion's)


Are you sure that's mandatory?

The standard says about integral promotions:
"An rvalue of type [...] short int, or unsigned short int can be
converted to an rvalue of type int if int can represent all the values
of the source type; otherwise, the source rvalue can be converted to an
rvalue of type unsigned int."
http://anubis.dkuug.dk/jtc1/sc22/ope...html#conv.prom

The words "can be" made me wonder: isn't it okay for a pair of short
operands to just remain short during the operation?

Thanks for all your replies so far,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #7
* "Niels Dekker (no reply address)" <un*****@this.is.invalid> schriebt:
Rob Williscroft wrote:

The type of short + short is int (standard intergral promotion's)


Are you sure that's mandatory?

The standard says about integral promotions:
"An rvalue of type [...] short int, or unsigned short int can be
converted to an rvalue of type int if int can represent all the values
of the source type; otherwise, the source rvalue can be converted to an
rvalue of type unsigned int."
http://anubis.dkuug.dk/jtc1/sc22/ope...html#conv.prom

The words "can be" made me wonder: isn't it okay for a pair of short
operands to just remain short during the operation?


Nope. What you're missing is §5/9 which defines the "usual arithmetic
conversions", and which for this case states that the integral promotions
_shall_ be applied. I read that as whatever is allowed as integral
promotion shall be applied, for otherwise the "shall" would be void of
meaning (personally I think the wording of §4.5 is YADITS).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #8
Victor Bazarov wrote:
Niels Dekker (no reply address) wrote:
Is it possible for a standard compliant C++ compiler to have
( sizeof(short) < sizeof(int) )
and ( sizeof(short) == sizeof((short)0 + (short)0) )
?

The latter is mandatory, AFAICT.


Wrong on my part. Integral promotions are performed. The result should
be of type 'int', therefore you cannot simultaneously have sizeof(short)
different and equal sizeof(int).
The former is definitely possible.


Victor
Jul 22 '05 #9
Howard wrote:

Rob's right, of course. Adding two shorts results in an int, not a short. [...] So whenever a short is smaller than an int, then the second comparison
should be false. (That is, a short should *also* be smaller than the sum of
two shorts.) Which is, I'm guessng, what you thought in the first place,
right?

Sorry for the misinformation. (And, what compiler gives you those
erroneous results?)


No problem. To me it just means that my question wasn't that silly! :-)
I haven't found a C++ compiler yet that says "short + short = short"
But maybe a C compiler will do... Because the C Standard says: "If
both operands have the same type, then no further conversion is needed."
http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/n2794.pdf

Apparently C and C++ have different "usual arithmetic conversions", and in C
(sizeof(short) == sizeof((short)0+(short)0)) might as well be true, even
if sizeof(short) < sizeof(int).

Best regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #10
Oops, I misinterpreted the C Standard:

I haven't found a C++ compiler yet that says "short + short = short"
But maybe a C compiler will do... Because the C Standard says: "If
both operands have the same type, then no further conversion is needed."
http://wwwold.dkuug.dk/jtc1/sc22/open/n2794/n2794.pdf

Apparently C and C++ have different "usual arithmetic conversions", and in C
(sizeof(short) == sizeof((short)0+(short)0)) might as well be true, even
if sizeof(short) < sizeof(int).


No, no, I was wrong! The C Standard says:
"Otherwise, the integer promotions are performed on both operands. Then
the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is
needed."

So only _after_ these integer promotions, there might be no further
conversion. But because of these promotions, both short operands are
promoted to int already.

Best regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
9
by: Fred Ma | last post by:
Hello, I've been trying to clear up a confusion about integer promotions during expression evaluation. I've checked the C FAQ and C++ FAQ (they are different languages, but I was hoping one...
10
by: Massimiliano Alberti | last post by:
Are there rules on how should the C behave with the arithmetic operations? Are the overflows always ignored? And are they simply truncated? (if I have a 16-bits unsigned short ints unsigned short...
112
by: Carsten Hansen | last post by:
Suppose I'm using an implementation where an int is 16 bits. In the program below, what function is called in the first case, and what is called in the second case? Also, if there is a difference...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
21
by: Frederick Gotham | last post by:
I set about trying to find a portable way to set the value of UCHAR_MAX. At first, I thought the following would work: #define UCHAR_MAX ~( (unsigned char)0 ) However, it didn't work for me....
6
by: sarathy | last post by:
Hi, What is integer promotion? How is it different from arithmetic conversion? Regards, Sarathy
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.