This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Crafting Better Source Code in C++


Eljay Love-Jensen <eljay@adobe.com> writes:

> Hi everyone,
> 
> #define SOAPBOX
> 
> I've been asked this question several times about C++ (in particular,
> GCC's C++), and I thought I'd share my thoughts.
> 
> The GCC compiler has a suite of warnings which will point out
> dangerous or suspicious coding practices.  By and large, I feel that
> these GCC warnings are indicative of either bad code ("bugs"!  ~10% of
> the time), or poor coding practices (~90%).
> 
> In either case, I think it is a worthwhile endeavor to use GCC's
> warning facilities to help improve the source code, and improve the
> portability of the source code.
> 
> At a minimum, I suggest everyone ALWAYS use these flags:
> -pedantic -Wall -W

As more and more C libraries incorporate C99 features, fewer and fewer
    people will be able to use -pedantic with C++ . Many real projects
    will need to seperate that code which can be written with strict
    conformance from that which requires non standard features, and
    use appropriately different compile flags.

While we are at it, however, I suggest another warning flag: -O
    :-) . Certain warnings (like 'foo unused', and 'foo may be used
    before it is initialized' ) require data-flow analysis, and so are
    only enabled with -On, e.g. -Wunitialized.

> 
> Other flags are documented here:
> http://gcc.gnu.org/onlinedocs/gcc-3.3.3/gcc/Warning-Options.html
> 
> I use almost all the additional flags.  Call me ultra-pedantic.  I
> wish there was an -WALL switch, to enable all warnings (even the
> annoying ones which I disregard), just so I wouldn't have the creepy
> feeling I've missed one.
[snip]

Compile this program:

    #include<iostream>
    #include<ostream>

    int main()
      {
        std::cout << "Hello World!" << std::endl;
      }

with: g++ -Weffc++ -Wsystem-headers

Alternatively, try building any boost-using C++ with -Weffc++, without
    -Wsystem-headers.

Personally, I think the flags not enabled by -W -Wall are only useful
    in very rare circumstances.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]