Bug 19968 - [3.3/3.4/4.0/4.1 Regression] Warning omitted for non-derived classes
Summary: [3.3/3.4/4.0/4.1 Regression] Warning omitted for non-derived classes
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.0
: P2 minor
Target Milestone: 3.4.4
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic, monitored
Depends on:
Blocks:
 
Reported: 2005-02-15 01:26 UTC by Volker Reichelt
Modified: 2005-03-02 21:54 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-02-26 18:42:02


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2005-02-15 01:26:41 UTC
Compiling the following code snippet with -Wextra, a sensible warning
is issued:

======================
struct B {};

struct A : B
{
    const int i;
};
======================

warn.cc:5: warning: non-static const member 'const int A::i' in class without a
constructor

However, if A is not a derived class, the warning is not given:

======================
struct A
{
    const int i;
};
======================

This is at least inconsistent. I'd vote for a warning in the second case, too.
Comment 1 Volker Reichelt 2005-02-15 01:29:19 UTC
Btw, the warning in the second case is missing since gcc 3.3.
Comment 2 Andrew Pinski 2005-02-15 01:32:08 UTC
: Search converges between 2002-10-02-trunk (#92) and 2002-10-03-trunk (#93).

Confirmed.
Comment 3 Alexandre Oliva 2005-03-02 21:36:35 UTC
I think this is correct behavior.  Without inheritance, the struct is POD, so
different rules apply.
Comment 4 Wolfgang Bangerth 2005-03-02 21:54:40 UTC
Correct. The class without inheritance doesn't need a constructor 
since objects of this type can be initialized using a brace-enclosed 
list. The class with inheritance is not POD, so it can't be initialized 
that way and needs a constructor. This should demonstrate this: 
----------------- 
struct B {}; 
 
struct A1 : B { const int i; }; 
struct A2     { const int i; }; 
 
A1 a1 = { 1 }; // not ok 
A2 a2 = { 1 }; // ok 
----------------- 
g/x> /home/bangerth/bin/gcc-3.4.4-pre/bin/c++ -W -Wall -ansi -pedantic -c x.cc 
x.cc:3: warning: non-static const member `const int A1::i' in class without a 
constructor 
x.cc:6: error: `a1' must be initialized by constructor, not by `{...}' 
 
This is therefore not a bug but correct behavior. 
 
W.