473,406 Members | 2,371 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,406 software developers and data experts.

What is the C++ type conversion precedence?

Hi,

Please see the following code and the output. It seems that if one of
%'s oprand is unsigned, the other will also be converted to unsigned.

There is a operator precedence table in wikipedia. I'm wondering what
is the general rule in type conversions.

Thanks,
Peng

#include <iostream>

int main() {
std::cout << -10 % 3 << std::endl;
std::cout << -10 % static_cast<unsigned>(3) << std::endl;
std::cout << static_cast<unsigned>(-10) % static_cast<unsigned>(3)
<< std::endl;
std::cout << 10 % 3 << std::endl;
}

$./main-g.exe
-1
0
0
1
Jul 17 '08 #1
5 3621
On Jul 17, 1:53 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Peng Yu:
Hi,
Please see the following code and the output. It seems that if one of
%'s oprand is unsigned, the other will also be converted to unsigned.

Yes. Usual promotion of arguments. In the standard known as the "usual
arithmetic conversions".

I tried to write a simplified explanation here but it seems the rules are not
amenable to simplification.

But the general idea is an automatic promotion up to some common type that
ideally should be able to represent all possible values of both arguments.
In the case of "-10 % static_cast<unsigned>(3)", I would think it is
as reasonable to convert it to
"-10 % static_cast<int>(static_cast<unsigned>(3))" as to
"static_cast<unsigned>(-10) % static_cast<unsigned>(3)".

But why C++ choose the latter one?
>
There is a operator precedence table in wikipedia. I'm wondering what
is the general rule in type conversions.

C++ is not based on operator precedence. However as a practical matter one can
often make do with thinking in terms of operator precedence. In effect, the
syntax rules are designed from the point of view of achieving operator
precedence for e.g. boolean, arithmetic and relational operators.
Jul 17 '08 #2
Peng Yu wrote:
On Jul 17, 1:53 pm, "Alf P. Steinbach" <al...@start.nowrote:
>* Peng Yu:
>>Hi,
Please see the following code and the output. It seems that if one of
%'s oprand is unsigned, the other will also be converted to unsigned.
Yes. Usual promotion of arguments. In the standard known as the "usual
arithmetic conversions".

I tried to write a simplified explanation here but it seems the rules are not
amenable to simplification.

But the general idea is an automatic promotion up to some common type that
ideally should be able to represent all possible values of both arguments.

In the case of "-10 % static_cast<unsigned>(3)", I would think it is
as reasonable to convert it to
"-10 % static_cast<int>(static_cast<unsigned>(3))" as to
"static_cast<unsigned>(-10) % static_cast<unsigned>(3)".

But why C++ choose the latter one?
Because that's what the Standard says to do. Probably because that's
how C did it.
Jul 18 '08 #3
On Jul 18, 12:24 am, Peng Yu <PengYu...@gmail.comwrote:
On Jul 17, 1:53 pm, "Alf P. Steinbach" <al...@start.nowrote:
In the case of "-10 % static_cast<unsigned>(3)", I would think
it is as reasonable to convert it to "-10 %
static_cast<int>(static_cast<unsigned>(3))" as to
"static_cast<unsigned>(-10) % static_cast<unsigned>(3)".
Reasonable doesn't count for much here. All that really counts
is the standard.
But why C++ choose the latter one?
Because that's what C did? And C choose the latter because they
had to choose one. Depending on the application, it is
sometimes better to preserve sign, and sometimes better to
preserve value. From my imperfect memory (and thus to be taken
with a grain of salt): K&R didn't really make it clear which one
was to be preferred, and early compilers varied (although if
memory serves me right, Johnson's pcc---the first C compiler to
"escape" from Bell Labs---preserved value). So no matter what
the C committee chose, it would break some code. I don't really
remember any of the arguments, but one thing that does occur to
me: the conversion of signed to unsigned is always well defined;
the conversion of unsigned to signed is implementation defined
(and may result in an implementation defined signal). While in
your example, it's obvious what the first would mean, try:
-10 - static_cast< unsigned >( -3 )
Rewriting it according to your first suggestion results in
implementation defined behavior (and possibly a signal);
rewriting it according to the second is well defined (for a
given size of int).

The whole thing is a mess, and the basic rule is to use int
unless there is a very strong reason to do otherwise, and to
limit unsigned types to cases where you need the modulo
arithmetic, or are doing bit manipulations (so that >is well
defined), or are accessing raw memory (as unsigned char). An
even more important rule, however, is not to mix signed an
unsigned, so faced with a poorly designed library, which uses
unsigned types for other things (like the number of elements in
an array), you should stick with unsigned types when interfacing
with it. (In sum, a real PITA.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 18 '08 #4
Peng Yu wrote:
Please see the following code and the output. It seems that if one of
%'s oprand is unsigned, the other will also be converted to unsigned.

There is a operator precedence table in wikipedia. I'm wondering what
is the general rule in type conversions.
Conversions/promotions take place first, then all operations are performed.
>
Thanks,
Peng

#include <iostream>

int main() {
std::cout << -10 % 3 << std::endl;
std::cout << -10 % static_cast<unsigned>(3) << std::endl;\
-10 is made unsigned, then % is evalutated. And since the division of
all the even powers of 2 minus 10 by 3 is exact (no remainder), you get
your 0 for your size of 'int', which is either 16 or 32 or 64...
std::cout << static_cast<unsigned>(-10) % static_cast<unsigned>(3)
<< std::endl;
Same thing here.
std::cout << 10 % 3 << std::endl;
}

$./main-g.exe
-1
0
0
1
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '08 #5
Peng Yu wrote:
[..]
In the case of "-10 % static_cast<unsigned>(3)", I would think it is
as reasonable to convert it to
"-10 % static_cast<int>(static_cast<unsigned>(3))" as to
"static_cast<unsigned>(-10) % static_cast<unsigned>(3)".

But why C++ choose the latter one?
Are you asking why the compiler does that or why the language is
specified the way it's specified?

The obvious answer to the former: because the Standard says so (5/9,
last bullet item). I do not really know the answer to the latter, but
if I had to guess, the reasons of simplicity would probably win.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '08 #6

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

Similar topics

4
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
3
by: Guybrush Threepwood | last post by:
double ** matrix; struct data_point { float x,y,z; float nx,ny,nz; float value; };
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
22
by: Tony Johansson | last post by:
Hello Experts! I'm reading i a book about C++ and they mention infix with telling what it is. I hope you out there can do so. Many thanks! //Tony
17
by: Anoob | last post by:
Can we consider () unary operator when calling a function, in exps eq. f(), ( 1 + 2). But when we call function f( 10 ) it is a binary operator. Even if we pass f( 10, 20) as we are using ,...
53
by: Deniz Bahar | last post by:
I know the basic definition of a sequence point (point where all side effects guaranteed to be finished), but I am confused about this statement: "Between the previous and next sequence point an...
19
by: Randy Yates | last post by:
Consider the following code: #include "dsptypes.h" /* definitions */ #define VECTOR_LENGTH 64 /* local variables */
4
by: Juan | last post by:
I'm migrating a VB.Net app to c# and found the following: Private m_State(,) As Integer If anyone knows what is the analogous in c#... is it an array? Thanks, Juan.
4
by: joshd | last post by:
can anyone help me figure out why when i run the following stored procedure i get the error: (1460 row(s) affected) Msg 245, Level 16, State 1, Procedure SP_SALESTRENDS, Line 40 Conversion...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...

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.