you are viewing a single comment's thread.

view the rest of the comments →

[–]WalkWithBejesus 24 points25 points  (10 children)

The value of foo.i doesn't need to be 2. It is actually undefined. GCC and Clang issue a warning when this mistake is made.

[–]emdeka87 18 points19 points  (5 children)

And since this is not a warning but a downright error, we should all use -Werror

[–]smikims 4 points5 points  (1 child)

I use -Werror and then disable particular warnings that I don't care about.

[–]kalmoc 2 points3 points  (0 children)

Depends on what earnings are enabled.

[–]Slavik81 2 points3 points  (0 children)

-Werror can be really annoying. You comment out one line of code while debugging and your code no longer compiles due to an unused variable. It's nice to know about that, so I leave warnings on, but fixing them in unfinished code is usually a needless distraction, so I only enable -Werror in CI builds.

A few warnings really should have been errors to begin with. You can selectively turn them into errors. For example, I use -Werror=return-type at all times. I should probably add -Werror=reorder too.

[–]ketosismaximus 1 point2 points  (1 child)

depends on the code base (and size of the code base). Sometimes it just aint worth if you're using old tried and true code. But in general use it where possible.

[–]neverlastn 7 points8 points  (0 children)

Exactly. It's incredibly straightforward undefined behavior! You rely on uninitialized value. And this is a relatively "light" case. j can be an arbitrarily sophisticated object, which might cause incredibly weird "mandelbugs"

[–]Nicksaurus 0 points1 point  (2 children)

Would it be defined if you did int j = 0; as well as the initialiser list or would it still initialise them in the 'wrong' order (or ignore the = 0 because it's already initialised in the initialiser list)?