Topic: Two questions about declarations in conditions
Author: sdouglas@armltd.co.uk (scott douglass)
Date: 1996/03/14 Raw View
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 ]