[Bug c++/47861] New: static variables inside member functions
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861 Summary: static variables inside member functions Product: gcc Version: 4.4.0 Status: UNCONFIRMED Severity: trivial Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: micha...@tiscali.it I know it has been probably discussed several times and probably eons ago. The language semanthics may induct to assume static variables defined in a member function are going to be modified only for a specifc class instance - obviously this is not happening. Not a c++ bug, a c++ trap.
[Bug c++/47861] static variables inside member functions
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861 --- Comment #7 from Michal Turlik 2011-02-24 09:44:53 UTC --- Well guys it is probably silly but you need to consider a more specific case. Base* MyClass::next(bool reset) { static Bases::iterator it = m_bases.begin(); if(reset) it = m_bases.begin(); Base* base = ((it != m_bases.end()) ? (*it++) : 0); return base; } void MyClass:: init() { Base* base = next(true); while(base) { base->init(); base = next(false); } } The default behaviour of init is to recall init on each child and each child will do the same if not specified differently. The issue is clearly caused by having declared static the iterator. Obviously the same snippet code can be replaced by a simple for each iterator construct. As you have mentioned before - static means static...consider only the implication of a code written in this way...a forced definition of the iterator being static would probably prevent some unprapred c++ users to make similar mistakes. Thank you all. Messaggio originale Da: gcc- bugzi...@gcc.gnu.org Data: 23/02/2011 22.28 A: Ogg: [Bug c++/47861] static variables inside member functions http: //gcc.gnu.org/bugzilla/show_bug.cgi?id=47861 --- Comment #6 from Jonathan Wakely 2011-02-23 21:28:10 UTC --- (In reply to comment #5) > Also it is hard to produce a warning when it comes to a failure that will only > be diagnostic at link time. IIUC I think the OP wants a warning for: struct S { int f(); }; int S::f() { static int i = 0; return ++i; } which doesn't require a definition and can't produce a linker error. I think adding a warning for this is silly.
[Bug c++/47861] static variables inside member functions
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861 --- Comment #10 from Michal Turlik 2011-02-24 11:15:51 UTC --- redi, the signatures where wrong, the correct ones are: Base* Base::next (bool) void Base::init() btw you got the point - I mean you understood how the code buggy is. a better warning should be: oo.cc:4:3: Warning: 'it' is declared as a function-scope static, you mean a static member variable? oo.cc:4:3: Warning: 'it' is declared as a function-scope static, error prone, see static member variables Honestly this would be prevent if the compiler was going to search for the static var definition. Messaggio originale Da: gcc-bugzi...@gcc.gnu.org Data: 24/02/2011 11.44 A: Ogg: [Bug c++/47861] static variables inside member functions http://gcc.gnu. org/bugzilla/show_bug.cgi?id=47861 --- Comment #8 from Jonathan Wakely 2011-02-24 10:44:15 UTC --- (In reply to comment #7) > The issue is clearly caused by having > declared static the iterator. So that's a bug, don't do that. > Obviously the same snippet code can be > replaced by a simple for each iterator construct. > As you have mentioned > before - static means static... consider only the implication of a code > written in this way...a > forced definition of the iterator being static > would probably prevent some unprapred c++ users to make similar > mistakes. I don't understand, are you now suggesting the local static should be turned into a static member by the compiler?! Again, please explain what warning you want. So far you've shown some buggy code, but haven't explained how GCC could help prevent users from creating that bug. e. g. foo.cc:4:3: In 'MyClass::next(bool)': foo.cc:4:3: Warning: 'it' is declared as a function-scope static, did you want to use a static member variable? For the record, I would be strongly-opposed to such a warning. There are a million ways to write code that is buggy, the compiler cannot possibly infer what you really wanted to do and suggest how to fix all bugs.
[Bug c++/47861] static variables inside member functions
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861 --- Comment #12 from Michal Turlik 2011-02-24 11:48:10 UTC --- Yep the Meyer singleton! T* instance() { static T instance; return &instance; } The trouble probably comes from this one...where the static definition comes from? Redi...I only tried to explain my point of view...you and the rest of the bugzilla team know what to do. Thank you all so much! Messaggio originale Da: gcc-bugzilla@gcc. gnu.org Data: 24/02/2011 12.27 A: Ogg: [Bug c++/47861] static variables inside member functions http://gcc.gnu. org/bugzilla/show_bug.cgi?id=47861 --- Comment #11 from Jonathan Wakely 2011-02-24 11:27:51 UTC --- (In reply to comment #10) > > oo.cc:4:3: Warning: 'it' is declared as a function- scope > static, error prone, see static member variables No, it is not "error prone" - it is valid C++, and even relied on by well-known idioms such as the "Meyers Singleton" Warning about it would be annoying and unhelpful. > Honestly this would > be prevent if the compiler was going to search for the static var > definition. I don't understand this sentence, sorry.