This is the mail archive of the gcc-bugs@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]

[Bug c++/14482] New: hard to avoid -Weffc++ warnings with union members


We've found -Weffc++ very useful in general but union
members produce a lot of noise about missing initializers.

With a named union member it's possible to call the default
constructor just to silence the warning, but with anonymous
unions it's impossible.  I've tried adding constructors to
the union just to check how that would work but that just
adds more warnings about those constructors not initializing
the entire union (actually doing so is an error!)

if possible, it would probably be better to just skip the
"should be initialized in the member initialization list"
warning for union members and for union constructors.

test program:

class Foo
{
public:
   enum { DBL, INTEGER } uniontype;
   
   // anonymous
   union {
      double dbl;
      int    integer;
   };
   
   // ordinary
   union {
      double named_dbl;
      int    named_integer;
   } named;

   // with constructor
   union with_ctor_union {
      double named_dbl;
      int    named_integer;
      with_ctor_union(int val)    : named_integer(val) {}
      with_ctor_union(double val) : named_dbl(val) {}
   };
   with_ctor_union with_ctor;

   Foo(int a)
      : uniontype(INTEGER),
        integer(a),
        named(),
        with_ctor(a)
   {
        named.named_integer = a;
   }

   Foo(double b)
      : uniontype(DBL),
        dbl(b),
        named(),
        with_ctor(b)
   {
      named.named_dbl = b;
   }
};

warnings:

uniont.cpp: In constructor `Foo::with_ctor_union::with_ctor_union(int)':
uniont.cpp:22: warning: `Foo::with_ctor_union::named_dbl' should be initialized 
in the member initialization list
uniont.cpp: In constructor `Foo::with_ctor_union::with_ctor_union(double)':
uniont.cpp:23: warning: `Foo::with_ctor_union::named_integer' should be 
initialized in the member initialization list
uniont.cpp: In constructor `Foo::Foo(int)':
uniont.cpp:32: warning: `Foo::<anonymous>' should be initialized in the member 
initialization list
uniont.cpp:32: warning: `Foo::<anonymous union>::dbl' should be initialized in 
the member initialization list
uniont.cpp: In constructor `Foo::Foo(double)':
uniont.cpp:41: warning: `Foo::<anonymous>' should be initialized in the member 
initialization list
uniont.cpp:41: warning: `Foo::<anonymous union>::integer' should be initialized 
in the member initialization list


Full gcc output:

Reading specs from /snapshot/lib/gcc/i386-unknown-freebsd4.9/3.4.0/specs
Configured with: ./configure --prefix=/snapshot
Thread model: posix
gcc version 3.4.0 20040303 (prerelease)
 /snapshot/libexec/gcc/i386-unknown-freebsd4.9/3.4.0/cc1plus -E -quiet -v 
uniont.cpp -Wall -Weffc++ -o uniont.ii
ignoring nonexistent directory "/snapshot/lib/gcc/i386-unknown-freebsd4.9/3.4.
0/../../../../i386-unknown-freebsd4.9/include"
#include "..." search starts here:
#include <...> search starts here:
 /snapshot/lib/gcc/i386-unknown-freebsd4.9/3.4.0/../../../../include/c++/3.4.0
 /snapshot/lib/gcc/i386-unknown-freebsd4.9/3.4.0/../../../../include/c++/3.4.
0/i386-unknown-freebsd4.9
 /snapshot/lib/gcc/i386-unknown-freebsd4.9/3.4.0/../../../../include/c++/3.4.
0/backward
 /usr/local/include
 /snapshot/include
 /snapshot/lib/gcc/i386-unknown-freebsd4.9/3.4.0/include
 /usr/include
End of search list.
 /snapshot/libexec/gcc/i386-unknown-freebsd4.9/3.4.0/cc1plus -fpreprocessed 
uniont.ii -quiet -dumpbase uniont.cpp -auxbase uniont -Wall -Weffc++ -version -o 
uniont.s
GNU C++ version 3.4.0 20040303 (prerelease) (i386-unknown-freebsd4.9)
        compiled by GNU C version 2.95.4 20020320 [FreeBSD].
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=130534
uniont.cpp: In constructor `Foo::with_ctor_union::with_ctor_union(int)':
uniont.cpp:22: warning: `Foo::with_ctor_union::named_dbl' should be initialized 
in the member initialization list
uniont.cpp: In constructor `Foo::with_ctor_union::with_ctor_union(double)':
uniont.cpp:23: warning: `Foo::with_ctor_union::named_integer' should be 
initialized in the member initialization list
uniont.cpp: In constructor `Foo::Foo(int)':
uniont.cpp:32: warning: `Foo::<anonymous>' should be initialized in the member 
initialization list
uniont.cpp:32: warning: `Foo::<anonymous union>::dbl' should be initialized in 
the member initialization list
uniont.cpp: In constructor `Foo::Foo(double)':
uniont.cpp:41: warning: `Foo::<anonymous>' should be initialized in the member 
initialization list
uniont.cpp:41: warning: `Foo::<anonymous union>::integer' should be initialized 
in the member initialization list
 as -o uniont.o uniont.s

-- 
           Summary: hard to avoid -Weffc++ warnings with union members
           Product: gcc
           Version: 3.4.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: arnej at europe dot yahoo-inc dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i386-unknown-freebsd4.9
  GCC host triplet: i386-unknown-freebsd4.9
GCC target triplet: i386-unknown-freebsd4.9


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14482


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