1

From what I understand, comparing two different types, including a short and a long will still cause a conversion. I believe that a short will be promoted to an int. However, I can't seem to find a direct answer on comparing a short to a long.

For example:

Is it improper to compare a Uint32 to a Uint8.

Is it improper to add a Uint32 to a Uint8?

Uint32/Uint8 are shorthand typedefs in SDL for uint32_t and uint8_t, respectively.

EDIT: I suppose I should be a bit more explicit on my overall question. I'm really wondering whether or not comparing or evaluating with two different types of ints, that are the same signage (in the example case, unsigned), but differ in the SIZE (uint8_t and uint32_t), is an improper thing to do.

Perhaps it is improper due to a implicit conversion. Perhaps it is improper because of a performance issue other than conversion. Maybe it is frowned upon because of some sort of readability issue I am unaware of.

In the comments two similar questions were linked, however they are comparing an int to a long. Which I believe is very similar, but doesn't an int just take the form of whichever version is needed (uint8_t, sint16_t, etc.)?

Lutz
  • 29
  • 1
  • 6
  • 1
    _"If the operands has arithmetic or enumeration type (scoped or unscoped), usual arithmetic conversions are performed on both operands following the rules for arithmetic operators."_ source and follow the links: http://en.cppreference.com/w/cpp/language/operator_comparison – Richard Critten Oct 03 '17 at 15:50
  • 1
    Comparison has the same [Usual Arithmetic Conversions](http://port70.net/~nsz/c/c11/n1570.html#6.3.1.8) as any other arithmetic operations. – Eugene Sh. Oct 03 '17 at 15:50
  • 4
    C != C++. Please only tag with one of the languages, unless both really are actually relevant. What are `Uint32` and `Uint8`? They aren't standard types in C++. – tambre Oct 03 '17 at 15:53
  • Possible duplicate of [What are the general rules for comparing different data types in C?](https://stackoverflow.com/questions/6636793/what-are-the-general-rules-for-comparing-different-data-types-in-c) – Support Ukraine Oct 03 '17 at 16:01
  • or dup of https://stackoverflow.com/questions/34568748/comparing-int-with-long-and-others – Support Ukraine Oct 03 '17 at 16:02
  • or dup of https://stackoverflow.com/questions/1010330/c-comparison-char-and-int – Support Ukraine Oct 03 '17 at 16:03
  • @4386427 All your dupe suggestions are tagged with C and therefore none of them are dupes. – Nir Friedman Oct 03 '17 at 16:21
  • Apologies to @tambre, I have not asked a question before. I fixed it as soon as I saw your comment. Thanks! – Lutz Oct 03 '17 at 16:31
  • Comparing integers of different types, but same sign-age is always OK and well defined and will provide the expected arithmetic compare. – chux - Reinstate Monica Oct 03 '17 at 16:40
  • @NirFriedman When I posted the dups the question was tagged C but if you search for c++ dups you'll probably find just as many – Support Ukraine Oct 03 '17 at 17:35
  • @4386427 Right, and now it's not, which is why I drew attention to that point. You should also remove your close vote; it's not likely to get closed anyhow but it's the right thing to do. – Nir Friedman Oct 03 '17 at 17:38

1 Answers1

1

I believe this question is answered with this: http://en.cppreference.com/w/cpp/language/operator_arithmetic under the subsection 'Conversions'.

Otherwise, the operand has integer type (because bool, char, char16_t, char32_t, wchar_t, and unscoped enumeration were promoted at this point) and integral conversions are applied to produce the common type, as follows:

If both operands are signed or both are unsigned, the operand with lesser conversion rank is converted to the operand with the greater integer conversion rank

So the overall answer to my question is, Yes, there is a conversion. And from what I can tell, there are no issues with comparing two unsigned int types, as long as you don't compared signed and unsigned.

Community
  • 1
  • 1
Lutz
  • 29
  • 1
  • 6
  • 1
    That's C, not C++, and thus cannot (by itself) be regarded as an answer to your question. – Nir Friedman Oct 03 '17 at 17:37
  • Thank you @NirFriedman I will update the answer accordingly. However, as far as I know, I cannot close this question as answered for two days unless someone else posts an answer. – Lutz Oct 03 '17 at 17:41