Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ allows implicit conversion from int to unsigned int?

Tags:

c++

Consider following code:

void foo(unsigned int x)
{

}

int main()
{
  foo(-5);
  return 0;
}

Compiles with no problems. Errors like this can cause lots of problems and are hard to find. Why C++ allows such conversion?

like image 928
Nefarel NefX Avatar asked Mar 10 '11 17:03

Nefarel NefX


People also ask

Does C allow implicit conversion?

Implicit Type Conversion is also known as 'automatic type conversion'. It is done by the compiler on its own, without any external trigger from the user. It generally takes place when in an expression more than one data type is present.

Why do we use unsigned in C?

Unsigned int is a data type that can store the data values from zero to positive numbers whereas signed int can store negative values also. It is usually more preferable than signed int as unsigned int is larger than signed int. Unsigned int uses “ %u ” as a format specifier.

What happens when you cast an int to an unsigned int?

You can convert an int to an unsigned int . The conversion is valid and well-defined. Since the value is negative, UINT_MAX + 1 is added to it so that the value is a valid unsigned quantity. (Technically, 2N is added to it, where N is the number of bits used to represent the unsigned type.)

What happens in implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.


1 Answers

The short answer is because C supported such conversions originally and they didn't want to break existing software in C++.

Note that some compilers will warn on this. For example g++ -Wconversion will warn on that construct.

In many cases the implicit conversion is useful, for example when int was used in calculations, but the end result will never be negative (known from the algorithm and optionally asserted upon).

EDIT: Additional probable explanation: Remember that originally C was a much looser-typed language than C++ is now. With K&R style function declarations there would have been no way for the compiler to detect such implicit conversions, so why bother restricting it in the language. For example your code would look roughly like this:

int foo(x)
unsigned int x
{

}

int main()
{
  foo(-5);
  return 0;
}

while the declaration alone would have been int foo(x);

The compiler actually relied on the programmer to pass the right types into each function call and did no conversions at the call site. Then when the function actually got called the data on the stack (etc) was interpreted in the way the function declaration indicated.

Once code was written that relied on that sort of implicit conversion it would have become much harder to remove it from ANSI C even when function prototypes were added with actual type information. This is likely why it remains in C even now. Then C++ came along and again decided to not break backwards compatibility with C, continuing to allow such implicit conversions.

like image 112
Mark B Avatar answered Sep 24 '22 18:09

Mark B