public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/14482] New: hard to avoid -Weffc++ warnings with union members
@ 2004-03-08 14:55 arnej at europe dot yahoo-inc dot com
  2004-03-08 15:37 ` [Bug c++/14482] " pinskia at gcc dot gnu dot org
  2004-04-28 19:06 ` pinskia at gcc dot gnu dot org
  0 siblings, 2 replies; 9+ messages in thread
From: arnej at europe dot yahoo-inc dot com @ 2004-03-08 14:55 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 9+ messages in thread
[parent not found: <bug-14482-8084@http.gcc.gnu.org/bugzilla/>]
[parent not found: <bug-14482-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2011-06-29 10:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-08 14:55 [Bug c++/14482] New: hard to avoid -Weffc++ warnings with union members arnej at europe dot yahoo-inc dot com
2004-03-08 15:37 ` [Bug c++/14482] " pinskia at gcc dot gnu dot org
2004-04-28 19:06 ` pinskia at gcc dot gnu dot org
     [not found] <bug-14482-8084@http.gcc.gnu.org/bugzilla/>
2009-04-28 23:42 ` pinskia at gcc dot gnu dot org
2009-12-10 10:56 ` brian dot amberg at unibas dot ch
2009-12-10 13:04 ` paolo dot carlini at oracle dot com
     [not found] <bug-14482-4@http.gcc.gnu.org/bugzilla/>
2011-06-29  9:10 ` nocannedmeat at gmail dot com
2011-06-29 10:23 ` redi at gcc dot gnu.org
2011-06-29 10:56 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).