Article View: comp.std.c++
Article #3676Two questions about declarations in conditions
From: scott douglass
Date: Thu, 14 Mar 1996 17:02
Date: Thu, 14 Mar 1996 17:02
75 lines
2700 bytes
2700 bytes
Hello,
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' */ }
}
}
2 -- the object is initialized and destroyed on each iteration, making 'f'
above eqivalent to:
void f(int i)
{
while (1) { T t = i; if (!t) break; /* do something with 't' */ }
}
Which is it supposed to be?
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' */ }
}
Which I prefer.
[I was tempted to suggest the tidier
condition:
expression
type-specifier-seq declarator initializer
but that would allow the potentially disagreeable '= { /*...*/ }' form.]
Thanks for your kind attention,
--scott
---
[ 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:
<sdouglas-1403961511110001@193.131.176.202>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backShow Full Headers
Complete Article Headers
From: sdouglas@armltd.co.uk (scott douglass) Message-ID: <sdouglas-1403961511110001@193.131.176.202> X-Original-Date: Thu, 14 Mar 1996 15:11:11 +0000 Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 14 Mar 96 17:02:20 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Two questions about declarations in conditions Organization: Apple Computer, Inc. X-Newsreader: Yet Another NewsWatcher 2.0.2 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMUhU8+EDnX0m9pzZAQFTbwF+LQxkylI5X4NRYqDYx1/VL94rak8aKkIb NwW5XKobI3Jg2TeiqRohKlMSowwJ1pem =UXRO