gcc-2.95.1: -Weffc++ is a little too strict about initialization

Graham Stoney greyham@research.canon.com.au
Mon Aug 30 00:56:00 GMT 1999


Hi folks,

The -Weffc++ flag in gcc-2.95.1 warns about constructs which break the rules
in Scott Myer's book "Effective C++". This is very cool. Rule 12 is "Prefer
initialization to assignment in constructors", so gcc-2.95.1 warns about any
member which isn't initialized in the member initialization list. This seem a
little excessive for plain old structure members which have no constructor of
their own and therefore cannot be initialized in the initialization list. Here
is an example with a class containing a POSIX thread mutex, which is a plain
old C structure, initialised either by structure assignment or a function call:

    #include <pthread.h>

    class C
    {
	pthread_mutex_t mutex;

    public:
	C::C();
    };

    C::C()
    {
	pthread_mutex_init(&mutex, NULL);
    }

gcc-2.95.1 gives:
    % g++ -Weffc++ weff.cc
    weff.cc: In method `C::C()':
    weff.cc:12: warning: `C::mutex' should be initialized in the member initialization list

It would be nice if gcc didn't warn about plain structure members which are
initialized in a constructor, given that they can't be initialized in the
member initialization list.

If it weren't for this, I could turn on -Werr too...

Thanks,
Graham


More information about the Gcc mailing list