3

I was trying to read through the C/C++ standard for this but I can't find the answer.

Say you have the following snippet:

int8_t m;
int64_t n;

And that at some point you perform m + n, the addition itself is a binary operator and I think the most likely think that happen in such a case is:

  1. Wide m to the same size of n, call the widening result m_prime
  2. Perform m_prime + n
  3. Return a result of type int64_t

I was trying to understand however if instead of performing m+n I had performed n+m the result would change (because maybe there could be a narrowing operations instead of a widening).

I cannot find the part of the standard that clarify this point (which I understand it could sound trivial).

Can anyone point me where I can find this in the standard? or what happens in general in situations like the one I exposed?

Personally I've been looking at the section "Additive operators" but it doesn't seem to me to explain what happens, pointer arithmetic is covered a bit, but there's no reference to some casting rule implicitly applied.

You can assume I'm talking about C++11, but any other standard I guess would apply the same rules.

user8469759
  • 2,522
  • 6
  • 26
  • 50
  • There is no `C/C++` standard. There's a `C` standard, an a `C++` standard. And the two are very different documents. – StoryTeller - Unslander Monica Nov 30 '16 at 14:42
  • C is basically almost a true subset of C++, at least ANSI C89 is. And yes, you get widening regardless of whether the smaller integer is to the left or to the right of the + sign. – Erik Alapää Nov 30 '16 at 14:44
  • 2
    *"C is basically almost a true subset of C++, at least ANSI C89 is"* - That isn't true, I'm afraid. – StoryTeller - Unslander Monica Nov 30 '16 at 14:47
  • @StoryTeller, I think that restricting to what I've asked they can be considered the same. I don't see why this specific part should change. – user8469759 Nov 30 '16 at 14:48
  • @StoryTeller. It is true. Note that I said 'almost'. All of K&R book examples (ANSI C89 version) compile with a C++ compiler. No two other languages I know of are that closely related. – Erik Alapää Nov 30 '16 at 14:48
  • Have a read on [Implicit conversions](http://en.cppreference.com/w/cpp/language/implicit_conversion), should explain pretty clearly what the rules are, references are in the footer. – sharyex Nov 30 '16 at 14:49
  • @ErikAlapää Could you point out the reference? Other than experimenting probably. – user8469759 Nov 30 '16 at 14:49
  • Because if the two can have different rules for `const` propagation, they can also have different rules for widening/narrowing conversions (and they do, gcc & clang have only C++ specific flags for any such warnings). – StoryTeller - Unslander Monica Nov 30 '16 at 14:49
  • @ErikAlapää -- You are [being misled and are misleading](http://stackoverflow.com/questions/31505402/does-c-contain-the-entire-c-language). That "almost" is a very big fraction. – StoryTeller - Unslander Monica Nov 30 '16 at 14:53
  • @user8469759: Maybe this will be enough? https://msdn.microsoft.com/en-us/library/fc9te331(v=vs.80).aspx – Erik Alapää Nov 30 '16 at 14:54
  • @StoryTeller: For most practical purposes, C (ANSI C89) is a true subset of C++. There are many C++ haters who want to disassociate C with C++, therefore they do not like that the languages are so close. – Erik Alapää Nov 30 '16 at 15:02
  • @ErikAlapää - For the practical purpose of `int *ptr = malloc(sizeof *ptr);` the two are not the same. That's valid (and actually good) `C` , and not valid `C++`. I don't hate on either. But I object to the notion one can just ignore the difference, it leads to sub-par programs and programmers. – StoryTeller - Unslander Monica Nov 30 '16 at 15:04
  • Didn't he say C is for all practical purposed a subset of C++. Your example isn't a valid C++ program. – djgandy Nov 30 '16 at 15:07
  • @StoryTeller This is a pretty good answer: (see the 'subset' question), https://isocpp.org/wiki/faq/c – Erik Alapää Nov 30 '16 at 15:19

1 Answers1

2

See Clause 5 Expressions [expr]. Point 10 starts

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

The sub-points that follow say things like "If either operand is...", "...the other shall...", "If both operands ..." etc.

For your specific example, see 10.5.2

Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.

Allison Lock
  • 2,375
  • 15
  • 17