2

If I write the code like this:

auto n = 2048 * 2048 * 5;
char* buf = new char[n];

So, Is auto deduction type safe from the integer overflow in C++17?

1 Answers1

7

2048 and 5 in C++ have a type, and that type is int. Multiplying two int's has a type and that type is int. There are values for which the result cannot fit in an int, and auto cannot prevent that.

What auto can prevent is accidentally narrowing the result, e.g.:

short x = 4 * 8192;
Jeff Garrett
  • 5,863
  • 1
  • 13
  • 12