[AD] C++ fix class and -Weffc++ compiler flag.

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Hello!
My language of choice is C++, and recently I've started using the -Weffc++
flag with gcc in order to help me find potential bugs in my programs. This
flag basically generates a warning whenever code is found that contradicts
the programming guidelines found in the book "Effective C++." The fact that
a flag was added to gcc just for this indicates how good the practices in
this
book really are, heh. This flag helps find lots of insidious little
potential bugs
and I really like it :)

Now, to cut to the chase: When using this flag, allegro's fix.h generates
quite a few warnings, all related to this block of code:

   fix()                                     { }
   fix(const fix &x)                         { v = x.v; }
   fix(const int x)                          { v = itofix(x); }
   fix(const long x)                         { v = itofix(x); }
   fix(const unsigned int x)                 { v = itofix(x); }
   fix(const unsigned long x)                { v = itofix(x); }
   fix(const float x)                        { v = ftofix(x); }
   fix(const double x)                       { v = ftofix(x); }

Basically, the warnings are generated because fix::v should be initialized
in
the member initialization list. Therefore, I propose that the above code be
replaced with the following:

   fix() : v(0)                              { }
   fix(const fix &x) : v(x.v)                { }
   fix(const int x) : v(itofix(x))           { }
   fix(const long x) : v(itofix(x))          { }
   fix(const unsigned int x) : v(itofix(x))  { }
   fix(const unsigned long x) : v(itofix(x)) { }
   fix(const float x) : v(ftofix(x))         { }
   fix(const double x) : v(ftofix(x))        { }

(Sorry, I don't know how to make patches :P)
The new code generates no warnings and is functionally equivalent to the
original, except for the fact that fix::v is initialized to 0 when the
default class
constructor is used. I think that this extremely slight loss in efficiency
is well
worth allowing us C++ coders to compile with the -Weffc++ flag without
warnings - also, using the member initialization list is considered to be
better
form than initializing variables in the body of the class constructor.

So, how about it - do I get to have my name appear in the changelog? ;)

Thanks,
Nick



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/