This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/2831: operator ?: requires storage for initialized static member constants
- To: gcc-gnats at gcc dot gnu dot org
- Subject: c++/2831: operator ?: requires storage for initialized static member constants
- From: fspam at sourceware dot cygnus dot com, at at sourceware dot cygnus dot com, home at sourceware dot cygnus dot com, dot at sourceware dot cygnus dot com, com at sourceware dot cygnus dot com
- Date: 15 May 2001 19:23:37 -0000
- Reply-To: fspam at sourceware dot cygnus dot com, at at sourceware dot cygnus dot com, home at sourceware dot cygnus dot com, dot at sourceware dot cygnus dot com, com at sourceware dot cygnus dot com
>Number: 2831
>Category: c++
>Synopsis: operator ?: requires storage for initialized static member constants
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Tue May 15 12:26:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Forest Wilkinson
>Release: gcc version 3.0 20010515 (prerelease)
>Organization:
>Environment:
Red Hat Linux 7.1 (kernel 2.4.2-2smp, dual processor i686)
>Description:
When compiling an executable from the included source code, g++ produces the following error messages:
/tmp/cce1LkFy.o: In function `main':
/tmp/cce1LkFy.o(.text+0xd): undefined reference to `foo_class::flag1 '
/tmp/cce1LkFy.o(.text+0x19): undefined reference to `foo_class::flag2 '
collect2: ld returned 1 exit status
I verified this with the gcc online compiler submission form at http://www.codesourcery.com/gcc-compile.shtml
The form said:
"The compiler used in this form was last updated from the nightly build on 2001-05-15 .
The sources were checked out from the gcc-3_0-branch branch of the GCC CVS repository."
The same problem occurs in the gcc that ships with red hat linux 7.1: gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
>How-To-Repeat:
// undefref.cpp
/*
When compiled with gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81),
or gcc version 3.0 20010515 (prerelease),
the compiler produces the following error message:
/tmp/cceI5nKb.o: In function `main':
/tmp/cceI5nKb.o(.text+0xd): undefined reference to `foo_class::flag1'
/tmp/cceI5nKb.o(.text+0x19): undefined reference to `foo_class::flag2'
collect2: ld returned 1 exit status
*/
class foo_class
{
public:
// flag1 & flag2 are initialized static integral constants,
// as described in Stroustrup 3: section 10.4.6.2: Member Constants.
// The are never used in a way that requires them to be stored as
// objects in memory, so they should not require a separate definition.
static const int flag1 = (1 << 0);
static const int flag2 = (1 << 1);
};
// According to Stroustrup, these should not be necessary:
//const int foo_class::flag1;
//const int foo_class::flag2;
int main( int argc, char *argv[])
{
// This line produces the "undefined reference" error:
int flags = (argc == 1) ? foo_class::flag1 : foo_class::flag2;
// This line is just fine:
int aflags = foo_class::flag1;
return 0;
}
>Fix:
I can work around this problem by avoiding the ?: operator when accessing my static member constants.
Another workaround for this example code would be to define storage for those constants, but this becomes ugly when they belong to a template class. (Besides, according to Stroustrup, I shouldn't have to do it.)
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/x-c++src; name="undefref.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="undefref.cpp"
Ci8vIHVuZGVmcmVmLmNwcAoKLyoKV2hlbiBjb21waWxlZCB3aXRoIGdjYyB2ZXJzaW9uIDIuOTYg
MjAwMDA3MzEgKFJlZCBIYXQgTGludXggNy4xIDIuOTYtODEpLApvciBnY2MgdmVyc2lvbiAzLjAg
MjAwMTA1MTUgKHByZXJlbGVhc2UpLAp0aGUgY29tcGlsZXIgcHJvZHVjZXMgdGhlIGZvbGxvd2lu
ZyBlcnJvciBtZXNzYWdlOgoKL3RtcC9jY2VJNW5LYi5vOiBJbiBmdW5jdGlvbiBgbWFpbic6Ci90
bXAvY2NlSTVuS2IubygudGV4dCsweGQpOiB1bmRlZmluZWQgcmVmZXJlbmNlIHRvIGBmb29fY2xh
c3M6OmZsYWcxJwovdG1wL2NjZUk1bktiLm8oLnRleHQrMHgxOSk6IHVuZGVmaW5lZCByZWZlcmVu
Y2UgdG8gYGZvb19jbGFzczo6ZmxhZzInCmNvbGxlY3QyOiBsZCByZXR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