Topic: Why's of C++ -- Part 1 (null declaration discussion)
Author: "Greg Brewer" <nospam.gregb@hal-pc.org>
Date: 1999/08/19 Raw View
Al Stevens <alstevens@midifitz.com> wrote in message
news:Lbiu3.8493$rZ2.158823@newscene.newscene.com...
> Once again. How about:
> for (;;)
> // ...
> }
> Or would you outlaw that idiom and require instead:
>
> while (true) {
> // ...
> }
for (;;) is an extreme case. How about
X *x = someverylongfunctioncall();
for (;x;x=x.Next())
x.DoSomething();
This is common for me becauseI like the for statement on a single line.
Greg Brewer
---
[ 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: "Al Stevens" <alstevens@midifitz.com>
Date: 1999/08/18 Raw View
>Someone posted asking why ";" was allowed as a null statement but not
>as a null declaration. The reply came back that ";" was "required"
>as a null statement. I was just pointing out that this supposed
>rationale was incorrect.
Once again. How about:
for (;;)
// ...
}
Or would you outlaw that idiom and require instead:
while (true) {
// ...
}
Ad infinitum.
[ 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: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1999/08/16 Raw View
"Al Stevens" <alstevens@midifitz.com> writes:
>>void foo(void) { printf("abc"); /**/; } // this is allowed
>>
>>struct Foo { int abc; /**/;}; // this is not allowed
>>
>>Can anyone explain why?
>
>The first usage is required. It represents a null statement that executes,
>sort of:
C & C++ have `{}' for a null statement, so allowing `;' as a null
statement too is not required. It's also potentially error-prone,
since it's easy to accidentally write something like
while (foo());
{
...
}
instead of
while (foo())
{
...
}
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/08/16 Raw View
In article <7p8ha3$g0e$1@mulga.cs.mu.OZ.AU>, Fergus Henderson
<fjh@cs.mu.OZ.AU> writes
>C & C++ have `{}' for a null statement, so allowing `;' as a null
>statement too is not required. It's also potentially error-prone,
>since it's easy to accidentally write something like
>
> while (foo());
> {
> ...
> }
Now I am getting confused. Are you suggesting that we outlaw the above
form of null statement? Unfortunately that is a bit late. According to
the C grammar an expression-statement is a statement. One form of that
is explicitly a null statement. The grammar for while is:
while ( expression ) statement
Hence the above code, however error prone, is legal.
>
>instead of
>
> while (foo())
> {
> ...
> }
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ 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: "Al Stevens" <alstevens@midifitz.com>
Date: 1999/08/16 Raw View
>C & C++ have `{}' for a null statement, so allowing `;' as a null
>statement too is not required. It's also potentially error-prone,
>since it's easy to accidentally write something like
So would you write:
for ({}{}) {
// ...
}
???
[ 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: Esa Pulkkinen <esap@cs.tut.fi>
Date: 1999/08/16 Raw View
> >C & C++ have `{}' for a null statement, so allowing `;' as a null
> >statement too is not required. It's also potentially error-prone,
> >since it's easy to accidentally write something like
"Al Stevens" <alstevens@midifitz.com> writes:
> So would you write:
> for ({}{}) { /* ... */ } ???
Maybe this is beside the point but the second semicolon in for(;;) is
not part of a null statement, only the first is. Also, the syntax only
allows expression-statement or simple-declaration for a
for-init-statement, neither of which allows "{}" to be used
there.
[Hmm.. gcc seems to allow "int i=0; for({}i<10;++i) { }", and only
warns about it with -pedantic. Is this yet another compatibility fix
or something?].
--
Esa Pulkkinen | C++ programmers do it virtually
E-Mail: esap@cs.tut.fi | everywhere with class, resulting
WWW : http://www.cs.tut.fi/~esap/ | in multiple inheritance.
[ 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: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1999/08/17 Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:
>Fergus Henderson <fjh@cs.mu.OZ.AU> writes
>>C & C++ have `{}' for a null statement, so allowing `;' as a null
>>statement too is not required. It's also potentially error-prone,
>>since it's easy to accidentally write something like
>>
>> while (foo());
>> {
>> ...
>> }
>
>Now I am getting confused. Are you suggesting that we outlaw the above
>form of null statement?
No.
>Unfortunately that is a bit late.
Agreed. (Hopefully future languages will do better, though.)
Someone posted asking why ";" was allowed as a null statement but not
as a null declaration. The reply came back that ";" was "required"
as a null statement. I was just pointing out that this supposed
rationale was incorrect.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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: "Al Stevens" <alstevens@midifitz.com>
Date: 1999/08/17 Raw View
>"Al Stevens" <alstevens@midifitz.com> writes:
>> So would you write:
>> for ({}{}) { /* ... */ } ???
>
>Maybe this is beside the point but the second semicolon in for(;;) is
>not part of a null statement, only the first is. Also, the syntax only
>allows expression-statement or simple-declaration for a
>for-init-statement, neither of which allows "{}" to be used
>there.
Of course. Fergus suggested that we don't need a null ; statement since we
have { }. My example tried to demonstrate with invalid code why I think we
do need a null ; statement, which your post seems to support.
---
[ 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: "Greg Brewer" <nospam.gregb@hal-pc.org>
Date: 1999/08/13 Raw View
For reasons I won't go into here, I spent some time creating a series of
macros that I used to declare the internal definitions for a structure. On
of the tricky parts was getting the semicolons just right. For some reason,
C++ and perhaps C does not allow a null declaration in the same way you can
have a null statement elsewhere. I.e.
void foo(void) { printf("abc"); /**/; } // this is allowed
struct Foo { int abc; /**/;}; // this is not allowed
Can anyone explain why?
Greg Brewer
---
[ 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: "David J. Littleboy" <davidjl@gol.com>
Date: 1999/08/13 Raw View
Greg Brewer wrote in message <7oui9d$1ajl$1@news.hal-pc.org>...
>For reasons I won't go into here, I spent some time creating a series of
>macros that I used to declare the internal definitions for a structure. On
>of the tricky parts was getting the semicolons just right. For some
reason,
>C++ and perhaps C does not allow a null declaration in the same way you can
>have a null statement elsewhere. I.e.
>
>void foo(void) { printf("abc"); /**/; } // this is allowed
>
>struct Foo { int abc; /**/;}; // this is not allowed
>
>Can anyone explain why?
A lone semicolon is a null statement, and a null statement, being _code_,
doesn't make sense in a struct declaration. I think, anyway...
David J. Littleboy
[ 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: "Al Stevens" <alstevens@midifitz.com>
Date: 1999/08/13 Raw View
>void foo(void) { printf("abc"); /**/; } // this is allowed
>
>struct Foo { int abc; /**/;}; // this is not allowed
>
>Can anyone explain why?
The first usage is required. It represents a null statement that executes,
sort of:
while(foo())
;
for (;;)
// ...
for (std::printf("abc");;)
// ...
etc.
The second usage serves no purpose.
[ 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 ]