Search code examples
c++c++11coding-stylecompiler-flags

What flag should I use to enforce a good C++11 style?


I am learning C++, and trying to write good code. I am currently using a lof of compiler flags, such as

-Wextra -Wall -pedantic -Werror -Wfatal-errors -Wcast-qual -Wcast-align -Wconversion -Wdouble-promotion -Wfloat-equal -Wshadow -Wpointer-arith -Weffc++ -ansi -Wstrict-aliasing

I have just learned that keywords new and delete should not be used anymore in C++11. However, I do not have any warnings when I use them.

Is there some flags to use to ensure a good C++11 style?


Solution

  • First, you have to realize that your compiler is never going to completely enforce good style.

    However, there are two techniques that you can use for the compiler to help you enforce your own style: warnings (which can be passed on the command line or in pragmas) and poison pragmas.

    It is difficult, however, to manage the exact set of warnings that are available with each compiler version, so I made a list of warnings. Note that support for clang is minimal, its warnings are usually inferior to gcc's in practive, despite their fantastic marketing; additionally it is impossible to detect which version of clang you are using to work around bugs (feature detection macros are rather worthless).

    I also made a list of poison.

    Additionally, I have some makefile magic that ensures that the above two techniques are applied to every applicable file as well as doing some other checks.

    It should be noted, however, that every application has different needs, so these headers should not be used as is. Very few applications, for example, would want to poison std::string like I did.