public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/47861] New: static variables inside member functions
@ 2011-02-23 16:50 michal.t at tiscali dot it
  2011-02-23 17:57 ` [Bug c++/47861] " paolo.carlini at oracle dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: michal.t at tiscali dot it @ 2011-02-23 16:50 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: michal.t@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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
@ 2011-02-23 17:57 ` paolo.carlini at oracle dot com
  2011-02-23 20:58 ` michal.tlk at gmail dot com
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-02-23 17:57 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2011.02.23 16:59:33
     Ever Confirmed|0                           |1

--- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-02-23 16:59:33 UTC ---
In any case, which action would you like to see (I'm not arguing the technical
point): a new warning? For which specific testcase? Otherwise I'm afraid we
have to close the PR, this is not the proper place for discussing present
and/or future C++ language features.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
  2011-02-23 17:57 ` [Bug c++/47861] " paolo.carlini at oracle dot com
@ 2011-02-23 20:58 ` michal.tlk at gmail dot com
  2011-02-23 21:20 ` redi at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: michal.tlk at gmail dot com @ 2011-02-23 20:58 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #2 from michal.tlk at gmail dot com 2011-02-23 20:45:23 UTC ---
Yep a warning stating the similarity of a static member function variable to
its counterpart as a static member would be appreciated...maybe when
pedantic option is going to be used.
The issue is real, just consider how the static members/attributes need to
be defined and how instead the static member function variables do not.
Thanks.

Hai ragione Carlo,
di certo non era il posto giusto per mettere tale notifica...un warning
potrebbe essere d aiuto...vi ringrazierei se potreste valutarne l
importanza.
la semantica del c++ porta a sottovalutare la staticita delle variabili.
Ad esempio potrebbe essere introddo un warning che notifichi l uguaglianza
di una variabile statica di funzione membro alla sua analoga versione
attributo di classe.
Grazie per la risposta immediata.
Buon lavoro.

M.

On Wed, Feb 23, 2011 at 5:59 PM, paolo.carlini at oracle dot com <
gcc-bugzilla@gcc.gnu.org> wrote:

> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861
>
> Paolo Carlini <paolo.carlini at oracle dot com> changed:
>
>           What    |Removed                     |Added
>
> ----------------------------------------------------------------------------
>             Status|UNCONFIRMED                 |WAITING
>   Last reconfirmed|                            |2011.02.23 16:59:33
>     Ever Confirmed|0                           |1
>
> --- Comment #1 from Paolo Carlini <paolo.carlini at oracle dot com>
> 2011-02-23 16:59:33 UTC ---
> In any case, which action would you like to see (I'm not arguing the
> technical
> point): a new warning? For which specific testcase? Otherwise I'm afraid we
> have to close the PR, this is not the proper place for discussing present
> and/or future C++ language features.
>
> --
> Configure bugmail: http://gcc.gnu.org/bugzilla/userprefs.cgi?tab=email
> ------- You are receiving this mail because: -------
> You reported the bug.
>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
  2011-02-23 17:57 ` [Bug c++/47861] " paolo.carlini at oracle dot com
  2011-02-23 20:58 ` michal.tlk at gmail dot com
@ 2011-02-23 21:20 ` redi at gcc dot gnu.org
  2011-02-23 21:22 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-23 21:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-02-23 20:55:08 UTC ---
(In reply to comment #2)
> Yep a warning stating the similarity of a static member function variable to
> its counterpart as a static member would be appreciated...maybe when
> pedantic option is going to be used.

No no no!
pedantic is for warning about using GCC extensions, not for warning about
possible misunderstanding of the language.

> The issue is real, just consider how the static members/attributes need to
> be defined and how instead the static member function variables do not.
> Thanks.

I've never encountered anyone who is confused in this way.  There is no such
thing as a "static member function variable" it is a function-scope static and
it has exactly the same semantics whether it is present in a member function or
a non-member function.

I don't see what providing definitions of static members has to do with it.

I don't see a need for a new warning here.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (2 preceding siblings ...)
  2011-02-23 21:20 ` redi at gcc dot gnu.org
@ 2011-02-23 21:22 ` redi at gcc dot gnu.org
  2011-02-23 21:28 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-23 21:22 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-02-23 20:57:57 UTC ---
As requested, please provide a code example showing what you mean, and explain
what warning you would like to be issued.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (3 preceding siblings ...)
  2011-02-23 21:22 ` redi at gcc dot gnu.org
@ 2011-02-23 21:28 ` pinskia at gcc dot gnu.org
  2011-02-23 22:09 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-02-23 21:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-02-23 21:19:56 UTC ---
Also it is hard to produce a warning when it comes to a failure that will only
be diagnostic at link time.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (4 preceding siblings ...)
  2011-02-23 21:28 ` pinskia at gcc dot gnu.org
@ 2011-02-23 22:09 ` redi at gcc dot gnu.org
  2011-02-24  9:52 ` michal.t at tiscali dot it
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-23 22:09 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (5 preceding siblings ...)
  2011-02-23 22:09 ` redi at gcc dot gnu.org
@ 2011-02-24  9:52 ` michal.t at tiscali dot it
  2011-02-24 10:57 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: michal.t at tiscali dot it @ 2011-02-24  9:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #7 from Michal Turlik <michal.t at tiscali dot it> 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-
bugzilla@gcc.gnu.org
Data: 23/02/2011 22.28
A: <michal.t@tiscali.it>

Ogg: [Bug c++/47861] static variables inside member functions

http:
//gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #6 from 
Jonathan Wakely <redi at gcc dot gnu.org> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (6 preceding siblings ...)
  2011-02-24  9:52 ` michal.t at tiscali dot it
@ 2011-02-24 10:57 ` redi at gcc dot gnu.org
  2011-02-24 11:20 ` paolo.carlini at oracle dot com
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-24 10:57 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (7 preceding siblings ...)
  2011-02-24 10:57 ` redi at gcc dot gnu.org
@ 2011-02-24 11:20 ` paolo.carlini at oracle dot com
  2011-02-24 11:28 ` michal.t at tiscali dot it
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-02-24 11:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|                            |INVALID

--- Comment #9 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-02-24 11:07:27 UTC ---
Seconded, Jon.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (8 preceding siblings ...)
  2011-02-24 11:20 ` paolo.carlini at oracle dot com
@ 2011-02-24 11:28 ` michal.t at tiscali dot it
  2011-02-24 11:34 ` redi at gcc dot gnu.org
  2011-02-24 13:13 ` michal.t at tiscali dot it
  11 siblings, 0 replies; 13+ messages in thread
From: michal.t at tiscali dot it @ 2011-02-24 11:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #10 from Michal Turlik <michal.t at tiscali dot it> 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-bugzilla@gcc.gnu.org

Data: 24/02/2011 11.44
A: <michal.t@tiscali.it>
Ogg: [Bug c++/47861] 
static variables inside member functions

http://gcc.gnu.
org/bugzilla/show_bug.cgi?id=47861

--- Comment #8 from Jonathan Wakely 
<redi at gcc dot gnu.org> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (9 preceding siblings ...)
  2011-02-24 11:28 ` michal.t at tiscali dot it
@ 2011-02-24 11:34 ` redi at gcc dot gnu.org
  2011-02-24 13:13 ` michal.t at tiscali dot it
  11 siblings, 0 replies; 13+ messages in thread
From: redi at gcc dot gnu.org @ 2011-02-24 11:34 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/47861] static variables inside member functions
  2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
                   ` (10 preceding siblings ...)
  2011-02-24 11:34 ` redi at gcc dot gnu.org
@ 2011-02-24 13:13 ` michal.t at tiscali dot it
  11 siblings, 0 replies; 13+ messages in thread
From: michal.t at tiscali dot it @ 2011-02-24 13:13 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47861

--- Comment #12 from Michal Turlik <michal.t at tiscali dot it> 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: <michal.t@tiscali.it>
Ogg: [Bug 
c++/47861] static variables inside member functions

http://gcc.gnu.
org/bugzilla/show_bug.cgi?id=47861

--- Comment #11 from Jonathan 
Wakely <redi at gcc dot gnu.org> 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.


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2011-02-24 11:48 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-02-23 16:50 [Bug c++/47861] New: static variables inside member functions michal.t at tiscali dot it
2011-02-23 17:57 ` [Bug c++/47861] " paolo.carlini at oracle dot com
2011-02-23 20:58 ` michal.tlk at gmail dot com
2011-02-23 21:20 ` redi at gcc dot gnu.org
2011-02-23 21:22 ` redi at gcc dot gnu.org
2011-02-23 21:28 ` pinskia at gcc dot gnu.org
2011-02-23 22:09 ` redi at gcc dot gnu.org
2011-02-24  9:52 ` michal.t at tiscali dot it
2011-02-24 10:57 ` redi at gcc dot gnu.org
2011-02-24 11:20 ` paolo.carlini at oracle dot com
2011-02-24 11:28 ` michal.t at tiscali dot it
2011-02-24 11:34 ` redi at gcc dot gnu.org
2011-02-24 13:13 ` michal.t at tiscali dot it

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).