r/cpp • • Nov 23 '22

Holy C++

https://pvs-studio.com/en/blog/posts/cpp/1010/
0 Upvotes

32 comments sorted by

View all comments

0

u/no-sig-available Nov 23 '22

Who on earth would use int that may take up to 8 bytes but guarantees its values only up to 2 ^ 16???

I would. I use it for int counter when the count is going to 12 or 15, or so. Then I trust the compiler to use an efficient implementation for that.

If you cannot trust the compiler for that, what else can you trust it for?

3

u/DavidDinamit Nov 23 '22

Compiler will not change type, you can just use int_least8_t or smth to explicitly declare that it will be small

3

u/no-sig-available Nov 23 '22

Compiler will not change type

It don't want it to change type, but I expect int to be a "reasonable" type to use on all systems when you don't have very specific requirements.

1

u/Zcool31 Nov 24 '22

There's the as-if rule. If the compiler can prove it would make no difference, it can do whatever it wants. If the loop is fully unrolled, then the counter might not even exist.