Article View: comp.std.c++
Article #3728Re: Two questions about declarations in condit
From: Steve Clamage
Date: Tue, 19 Mar 1996 00:15
Date: Tue, 19 Mar 1996 00:15
77 lines
3412 bytes
3412 bytes
In article 1403961511110001@193.131.176.202, sdouglas@armltd.co.uk
(scott douglass) writes:
>I read section 6.4 carefully but I couldn't decide what the lifetime (not
>scope) of an object declared in the condition of a while or for statements
>is. Reading D&E 3.11.5.2 didn't help either. Given the following:
>struct T { T(int); ~T(); operator bool() const; /*...*/ };
>
>void f(int i)
> {
> while (T t = i) { /* do something with 't' */ }
> }
>There are two "obvious" possibilities, I9m leaning toward the first:
>1 -- The object is initialized just once and destroyed just once, making
>'f' above eqivalent to:
>void f(int i)
> {
> {
> T t = i;
> while (t) { /* do something with 't' */ }
> }
> }
Interesting question, and I believe your preferred interpretation is
correct. Here's why: It is clear from the definition of "while" that
the controlled statements that are iterated are those which follow the
condition; the condition is not part of the iterated statement or block.
Further, the *value* of the condition determines whether the controlled
statements are executed. The condition is not a statement, and so
only its value is recomputed; the definition is not re-executed.
Besides, it would be rather pointless if in your example 't' was set to
'i' each time around the loop.
>Bonus question: why does the grammer allow only the '=
>assignment-expression' form:
> condition:
> expression
> type-specifier-seq declarator = assignment-expression
>instead of:
> condition:
> expression
> type-specifier-seq declarator = assignment-expression
> type-specifier-seq declarator ( expression-list )
>So that I could write:
>void f(int i)
> {
> while (T t(i)) { /* do something with 't' */ }
> }
I think the general case could lead to ambiguities in parsing. If a
function call can possibly be interpreted as a variable definition, then
it is a variable definition. That could lead to unexpected behavior
which would be difficult to track down.
You can always write the same thing with "=" notation:
while( T t = T(i) ) { ... }
This rewrite is not guaranteed to be as efficient, but most compilers
do optimize away the extra copy.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<4ikkbu$b5u@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<sdouglas-1403961511110001@193.131.176.202>Show Full Headers
Complete Article Headers
From: clamage@Eng.Sun.COM (Steve Clamage) Message-ID: <4ikkbu$b5u@engnews1.Eng.Sun.COM> X-Original-Date: 18 Mar 1996 21:25:18 GMT Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 19 Mar 96 00:15:53 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: Two questions about declarations in condit Organization: Sun Microsystems Inc. References: <sdouglas-1403961511110001@193.131.176.202> Reply-To: clamage@Eng.Sun.COM X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMU39E+EDnX0m9pzZAQEZWwF+K8qE2YkbTvCe1mg+j4WgcD5mnkJdYySj Vzgk7Pjsv+FO+sBNGVf+x2hcS06Eto4r =BWJI