Patch for static data members
Mark Mitchell
mmitchell@usa.net
Tue Jan 13 12:40:00 GMT 1998
Here's a patch which forbids duplicate initialization of static data
members, on code like this:
struct S { static const int i = 3; }
const int S::i = 2;
This used to be silently accepted (and gave i the value 3).
--
Mark Mitchell mmitchell@usa.net
Stanford University http://www.stanford.edu
Index: gcc/cp/decl.c
===================================================================
RCS file: /home/mitchell/Repository/egcs/gcc/cp/decl.c,v
retrieving revision 1.13
diff -c -p -r1.13 decl.c
*** decl.c 1998/01/12 03:20:50 1.13
--- decl.c 1998/01/13 18:52:08
*************** start_decl (declarator, declspecs, initi
*** 6089,6094 ****
--- 6089,6101 ----
context, DECL_NAME (decl));
DECL_CONTEXT (decl) = DECL_CONTEXT (field);
}
+ /* Static data member are tricky; an in-class initialization
+ still doesn't provide a definition, so the in-class
+ declaration will have DECL_EXTERNAL set, but will have an
+ initialization. Thus, duplicate_decls won't warn
+ about this situation, and so we check here. */
+ if (DECL_INITIAL (decl) && DECL_INITIAL (field))
+ cp_error ("duplicate initialization of %D", decl);
if (duplicate_decls (decl, field))
decl = field;
}
Index: gcc/testsuite/g++.old-deja/g++.other/static1.C
===================================================================
RCS file: static1.C
diff -N static1.C
*** /dev/null Mon Dec 31 20:00:00 1979
--- static1.C Tue Jan 13 10:52:38 1998
***************
*** 0 ****
--- 1,8 ----
+ extern "C" void abort();
+
+ struct S
+ {
+ static const int i = 3;
+ };
+
+ const int S::i = 2; // ERROR - duplicate initialization
More information about the Gcc-bugs
mailing list