jeudi 26 octobre 2017

Why doesn't compiler generate warning for negative value with size_t? [duplicate]

This question already has an answer here:

In the following code, I have used size_t as an function argument and passed negative value. I have compiled program on GCC(Linux) using following command.

g++ -Wall size.cpp -o size

GCC compiled successful without warning, but the results are not what I expect:

size_t : 18446744073709551615
int : -1

Code:

#include <iostream>

void func1(size_t i) 
{
  std::cout << "size_t : " << i << std::endl;
}

void func2(int i) 
{
  std::cout << "int : " << i << std::endl;
}

int main() 
{
  func1(-1);
  func2(-1);
  return 0;
}

Why doesn't compiler generate warning for negative value with size_t?

Aucun commentaire:

Enregistrer un commentaire