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

后端 未结 5 1623
野的像风
野的像风 2020-12-07 00:40

Consider following code:

void foo(unsigned int x)
{

}

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

Compiles with no problems. Errors like this c

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 01:20

    Because the standard allows implicit conversion from signed to unsigned types.

    Also (int)a + (unsigned)b results to unsigned - this is a c++ standard.

提交回复
热议问题