Topic: static variable in 'if' condition


Author: Wade Holst <wade@csd.uwo.ca>
Date: 1999/11/27
Raw View

haim_kreitman@my-deja.com writes:

> I have the following code:
>
> extern int foo();
>
> void main() {
>     if (static int x = foo()) {
>         // here I use static variable x
>     }
> }

Related to James' comments, the semantics of the above merits
a warning/error.  If 'static' is to work consistently, then
the semantics in the above contex should match with the semantics
of a static local variable in a function, in which it is
initialized once (not each time the function is called).

For example, if we modify the above example slightly:
      void main() {
   for (int i = 0; i < 10; ++i ) {
       if (static int x = foo()) {
    // here I use static variable x
          }
          }
      }

Then I would expect that 'foo' would only be called ONCE.
If James's comments hadn't answered the question, then which
compiler is "right" would depend on whether you thought the
above actions were right or wrong - should foo be called 1 time
or 10 times?

Wade




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/24
Raw View
haim_kreitman@my-deja.com wrote:
>
> I have the following code:
>
> extern int foo();
>
> void main() {
>     if (static int x = foo()) {
>         // here I use static variable x
>     }
> }
>
> This code compiled in Visual C++ 6.0 and CAN'T be
> compiled in Borland C++ Builder 4.0
>
> The 'static' specifier is problematic.
> I checked ANSI C++ Draft and I couldn't find
> any restriction for x to be static.
> Who is right Visual C++ 6.0 or Borland C++ Builder 4.0 ?

The relevant grammar rules are in section 6.4 p1:
 _selection-statement_:
  if ( _condition_ ) _statement_
  if ( _condition_ ) _statement_ else _statement_
  switch ( _condition_ ) _statement_

 _condition_:
  _expression_
  _type-specifier-seq_ _declarator_ = _assignment-expression_

'static' isn't a type-specifier, it's a storage class specifier.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: haim_kreitman@my-deja.com
Date: 1999/11/24
Raw View
I have the following code:

extern int foo();

void main() {
    if (static int x = foo()) {
        // here I use static variable x
    }
}

This code compiled in Visual C++ 6.0 and CAN'T be
compiled in Borland C++ Builder 4.0

The 'static' specifier is problematic.
I checked ANSI C++ Draft and I couldn't find
any restriction for x to be static.
Who is right Visual C++ 6.0 or Borland C++ Builder 4.0 ?


Sent via Deja.com http://www.deja.com/
Before you buy.






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/24
Raw View
haim_kreitman@my-deja.com wrote:
>
> I have the following code:
>
> extern int foo();
>
> void main() {
>     if (static int x = foo()) {
>         // here I use static variable x
>     }
> }
>
> This code compiled in Visual C++ 6.0 and CAN'T be
> compiled in Borland C++ Builder 4.0
>
> The 'static' specifier is problematic.
> I checked ANSI C++ Draft and I couldn't find
> any restriction for x to be static.
> Who is right Visual C++ 6.0 or Borland C++ Builder 4.0 ?

The relevant grammar rules are in section 6.4 p1:
 _selection-statement_:
  if ( _condition_ ) _statement_
  if ( _condition_ ) _statement_ else _statement_
  switch ( _condition_ ) _statement_

 _condition_:
  _expression_
  _type-specifier-seq_ _declarator_ = _assignment-expression_

'static' isn't a type-specifier, it's a storage class specifier.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]