Topic: functional-style cast ambiguity
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Tue, 1 Mar 1994 14:12:59 GMT Raw View
daniels@biles.com (Brad Daniels) writes:
>When I first started looking at C++, someone mentioned to me that function-
>style casts require the compiler to decide late in the semantic phase whether
>a given statement is a declaration or an expression using the comma operator.
>I was thinking about the situation, trying to understand it fully, and came
>up with this example:
>
>#include <iostream.h>
>
>main() {
> double i=0,j=0;
> {
> int (i), j=1.5; // (A) Ambiguous - Comma expression or declaration?
Declaration.
> i=1.5;
> cerr<<i<<" "<<j<<"\n";
> }
> int (i), (j=1.5); // (B) unambiguously a comma expression
> int (i), j=1.5; // (C) No error if interpreted as a comma expression,
> // error if interpreted as a declaration.
>}
>
>DEC C++ interprets (C) as a declaration, resulting in a failure to compile.
>Commenting line (C) out causes the resulting program to print out "1 1",
>indicating that line (A) was again interpreted as a declaration.
This is correct.
>HP C++ (Cfront-based) interprets all three of line (A), (B), and (C)
>as comma expressions. (It compiles cleanly and prints out "1.5 1.5".)
HP C++ is wrong.
>What is the correct behavior here, if there is such a thing? Has it
>even been discussed by the committee?
See ARM 6.8, "Ambiguity Resolution". If something could be parsed as
both a declaration and an expression, it is a declaration.
--
Fergus Henderson - fjh@munta.cs.mu.oz.au
Author: daniels@biles.com (Brad Daniels)
Date: Wed, 23 Feb 1994 18:12:12 GMT Raw View
When I first started looking at C++, someone mentioned to me that function-
style casts require the compiler to decide late in the semantic phase whether
a given statement is a declaration or an expression using the comma operator.
I was thinking about the situation, trying to understand it fully, and came
up with this example:
#include <iostream.h>
main() {
double i=0,j=0;
{
int (i), j=1.5; // (A) Ambiguous - Comma expression or declaration?
i=1.5;
cerr<<i<<" "<<j<<"\n";
}
int (i), (j=1.5); // (B) unambiguously a comma expression
int (i), j=1.5; // (C) No error if interpreted as a comma expression,
// error if interpreted as a declaration.
}
DEC C++ interprets (C) as a declaration, resulting in a failure to compile.
Commenting line (C) out causes the resulting program to print out "1 1",
indicating that line (A) was again interpreted as a declaration.
HP C++ (Cfront-based) interprets all three of line (A), (B), and (C)
as comma expressions. (It compiles cleanly and prints out "1.5 1.5".)
What is the correct behavior here, if there is such a thing? Has it
even been discussed by the committee?
Thanks,
- Brad
-------------------------------------------------------------------------------
+ Brad Daniels | "There's only one sweeping generalization +
+ Biles and Associates | which is completely true, and this is it." +
+ These are my views, not B&A's | - Me (1994) +
-------------------------------------------------------------------------------