3

I used to select the default warning level in C++ programming. For example, in VS, the default warning level is Level3 (/W3) and No (/WX-) (don't treat warnings as errors).

I am wondering is it a good practice to choose highest warning level in C++ programming, e.g. EnableAllWarnings (/Wall) with Yes (/WX) (treat warnings as errors)?

Any advice will be much appreciated.

8

The compilers are usually very good in spotting mistakes, therefore I always enable all possible warnings and errors (-Wall and -Wextra for gcc), and turn warnings into errors. This way nothing gets through.

Also, I try to write as standard compliant code as possible (-pedantic for gcc), meaning I try to avoid compiler extensions.

Sometimes it is not possible, because of an external library. Then you are forced to put required settings to make it compile. Unfortunately this usually goes with lots of warnings.

| improve this answer | |
  • Is it worth to fix every single warning, e.g. signed/unsigned mismatch? – herohuyongtao Mar 17 '14 at 8:06
  • 2
    @herohuyongtao The latest compilers are quite good. Of course they do have problems. But I tend to believe that they tend to know better then me if there is a mistake. This is not always true, due to compiler bugs, but the cases when it is not true are quite rare. – BЈовић Mar 17 '14 at 8:08
  • "[compilers] tend to know better then me if there is a mistake / not always true, due to compiler bugs" - it's not just about compiler bugs... you may know that your values won't span the range of a data type and therefore no actual signed/unsigned issues will occur (e.g. a vector of students in a class will never have an index that can't be stored in int) but it's still sloppy practice, or you may know that you really did mean to say if (x = y)... having to write if ((x = y)) is still a good practice as people who know the compiler warnings may understand that means it's intentional. – Tony Mar 19 '14 at 4:02
  • @Tony That is correct. Compilers are not going to warn you for plain stupidity. That is what unit, functional, integration, acceptance tests are for. – BЈовић Mar 19 '14 at 7:24
4

Generally speaking, yes, it is a good idea to raise warnings to the highest level possible.

You may get some warnings that are overly picky or that just don't worry you in which case you can explicitly pragma them away in specific locations. At least then you are making a specific and explicit decision to ignore the warning.

Warnings as errors is more contentious in my experience. Some people think it is very important to have turned on. I personally keep this turned off to allow a little more flexibility while I am writing code, but then I trust myself not to ignore warnings, and certainly never commit code containing warnings. I have in the past however turned this on to catch programmers who were not quite as conscientious as they should have been.

| improve this answer | |
  • 1
    "I have in the past however turned this on to catch programmers who were not quite as conscientious as they should have been." +1! – Panzercrisis Mar 17 '14 at 13:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.