Topic: if ( static bool firstTime = 1 ) ??


Author: Marcus Barnes <marcus@nospam.multigen.com>
Date: 1998/07/23
Raw View
Hi All,

Microsoft Visual C++ v5.0 permits this construct:

void foo ()
{
    if ( static bool firstTime = true ) { do_something(); firstTime =
false; }
    //   ^^^^^ ILLEGAL??
}



Author: James Kuyper <kuyper@wizard.net>
Date: 1998/07/23
Raw View
Marcus Barnes wrote:
>
> Hi All,
>
> Microsoft Visual C++ v5.0 permits this construct:
>
> void foo ()
> {
>     if ( static bool firstTime = true ) { do_something(); firstTime =
> false; }
>     //   ^^^^^ ILLEGAL??
> }
>
> From reading the draft standard, I believe including a storage specifier
> is illegal here. Microsoft's documentation doesn't mention it as an
> extension. Can someone please confirm my beliefs on this?

You're correct. If the condition in an if() statement isn't an ordinary
exprssion, it takes a form that looks like a simple declaration, but
differs from it in that the only declaration specifiers allowed are type
specifiers, not storage class specifiers like 'static'. The other
differences include that there must be at least one type-specifier,
exactly one declarator, and the part which looks like an initializer is
not optional and can only take the form "= assignment-expression".

See section 6.4, which also says that "the type-specifier-seq shall not
contain typedef and shall not declare a new class or enumeration".


[ 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: "Jim Cobban" <Jim.Cobban.jcobban@nt.com>
Date: 1998/07/23
Raw View
In article <35B673BE.C194CB50@nospam.multigen.com>,
Marcus Barnes  <marcus@multigen.com> wrote:
>Microsoft Visual C++ v5.0 permits this construct:
>
>void foo ()
>{
>    if ( static bool firstTime = true ) { do_something(); firstTime =
>false; }
>    //   ^^^^^ ILLEGAL??
>}
>
>From reading the draft standard, I believe including a storage specifier
>is illegal here. Microsoft's documentation doesn't mention it as an
>extension. Can someone please confirm my beliefs on this?

I agree with your interpretation.  The compiler should flag this as an
extension.  If you wish your code to be portable, don't use this.

--
Jim Cobban   |  jcobban@nortel.ca                   |  Phone: (613) 763-8013
Nortel (MCS) |                                      |  FAX:   (613) 763-5199


[ 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              ]