Topic: new expression as left operand of ->
Author: Greg Herlihy <greghe@pacbell.net>
Date: Fri, 5 Oct 2007 16:49:25 CST Raw View
On Oct 4, 1:54 pm, James Kanze <james.ka...@gmail.com> wrote:
> Is the following program legal C++:
>
> struct B
> {
> B* f() { return this ; }
> } ;
>
> int
> main()
> {
> B* p = new B()->f() ; // line 10
> return 0 ;
> }
>
> I'm particularly interested in line 10. Most compilers (but not
> g++) seem to accept it, and it seems reasonable to accept it,
> but formally, the standard says that the left operand of -> must
> be a postfix-expression, and of course, a new-expression is not
> a postfix-expression.
No, the program is not legal C++ because line 10 contains an invalid
expression. The arrow operator (->), being a "postfix expression" has
a higher precedence than the "new declarative", so a C++ compiler
should effectively parses line 10 as:
B * p = new (B()->f());
which is a syntax error because the left operand for the arrow
operator must be a pointer - and B() is not a pointer. Changing the
order of evaluation by adding parentheses, like so:
B * p = (new B())->f();
ensures that the left operand for the arrow operator is a pointer -
and therefore would fix the syntax error.
Moreover I found only one C++ compiler (EDG/C++) that compiled line 10
as-is. Presumably, EDG is supplying its own parentheses as a
convenience to the programmer. Though strictly speaking, parentheses
are needed for line 10 to compile as intended. Every other C++
compiler that I tested: gcc (v4.01), Microsoft Visual C++ (v8.0),
Comeau C++ compiler (v4.3.9), and the Intel C++ compiler (v10.0) - all
reported more or less the same error.
In fact the Comeau C++ compiler (v4.3.9) and Intel C++ (v10.0)
compiler gave nearly identical error messages:
error #1725:
this operator is not allowed at this point; use parentheses
new B()->f();
^
> So is it intentional that this is
> illegal, and most compilers get it wrong, or is it an accidental
> side effect of the way the standard is formulated, and most
> compilers implement the actual intent. (The point, of course,
> is that a new-expression is delimited by the keyword new on the
> left, and by the limitations on the structure of a new-type-id
> and the new-initializer on the right, so the above really
> creates no ambiguity nor parsing problem.)
I would say that the original C++ program is not legal, and that
nearly all (current) C++ compilers do issue an appropriate diagnostic.
The EDG compiler is certainly free to compile the program anyway
(after all, the intended effect of the statement is clear).
Nonetheless, from my understanding of the requirements, the EDG
compiler should nonetheless also issue a diagnostic like the others.
Greg
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Sat, 6 Oct 2007 11:41:25 CST Raw View
On Oct 6, 12:49 am, Greg Herlihy <gre...@pacbell.net> wrote:
> On Oct 4, 1:54 pm, James Kanze <james.ka...@gmail.com> wrote:
> > Is the following program legal C++:
> > struct B
> > {
> > B* f() { return this ; }
> > } ;
> > int
> > main()
> > {
> > B* p = new B()->f() ; // line 10
> > return 0 ;
> > }
> > I'm particularly interested in line 10. Most compilers (but not
> > g++) seem to accept it, and it seems reasonable to accept it,
> > but formally, the standard says that the left operand of -> must
> > be a postfix-expression, and of course, a new-expression is not
> > a postfix-expression.
> No, the program is not legal C++ because line 10 contains an invalid
> expression. The arrow operator (->), being a "postfix expression" has
> a higher precedence than the "new declarative", so a C++ compiler
> should effectively parses line 10 as:
> B * p = new (B()->f());
It can't do that either. It's not question of precedence (the
standard doesn't specify precedence); what follows the new must
be a new-type-id, optionally followed by a new-initializer. It
is precisely because the grammar requires the new expression to
end after the () that it would "seem like" the expression should
be legal (and why many compilers have no problems with it). The
compiler must in fact parse this as (new B())->f(), and then
decide that the left hand argument of the -> is not a legal
production, even though it is well delimited and has the correct
type. This seems like an artificial restriction to me.
> which is a syntax error because the left operand for the arrow
> operator must be a pointer - and B() is not a pointer. Changing the
> order of evaluation by adding parentheses, like so:
> B * p = (new B())->f();
> ensures that the left operand for the arrow operator is a pointer -
> and therefore would fix the syntax error.
That's the (trivial) work around, and what I am doing.
> Moreover I found only one C++ compiler (EDG/C++) that compiled
> line 10 as-is.
That's a third. Sun CC and VC++ had no problems with it.
> Presumably, EDG is supplying its own parentheses as a
> convenience to the programmer.
More likely, the authors simply parsed according to the grammar
of new, and didn't do any special checking. If you parse
normally, you need an extra, special check that the left hand
production of the -> is not a new expression.
> Though strictly speaking, parentheses
> are needed for line 10 to compile as intended. Every other C++
> compiler that I tested: gcc (v4.01), Microsoft Visual C++ (v8.0),
> Comeau C++ compiler (v4.3.9), and the Intel C++ compiler (v10.0) - all
> reported more or less the same error.
I'm pretty sure that it compiled with my version of VC++ (8),
but I'll reverify Monday. It definitly compiled with Sun CC.
And I don't get the other two: you said it compiled with EDG,
but Comeau and Intel are both EDG.
> In fact the Comeau C++ compiler (v4.3.9) and Intel C++ (v10.0)
> compiler gave nearly identical error messages:
(They use the same front end.)
> error #1725:
> this operator is not allowed at this point; use parentheses
> new B()->f();
> ^
In other words, they have a special check as well, like g++.
> > So is it intentional that this is
> > illegal, and most compilers get it wrong, or is it an accidental
> > side effect of the way the standard is formulated, and most
> > compilers implement the actual intent. (The point, of course,
> > is that a new-expression is delimited by the keyword new on the
> > left, and by the limitations on the structure of a new-type-id
> > and the new-initializer on the right, so the above really
> > creates no ambiguity nor parsing problem.)
> I would say that the original C++ program is not legal, and that
> nearly all (current) C++ compilers do issue an appropriate diagnostic.
> The EDG compiler is certainly free to compile the program anyway
> (after all, the intended effect of the statement is clear).
> Nonetheless, from my understanding of the requirements, the EDG
> compiler should nonetheless also issue a diagnostic like the others.
It's quite clear that the original C++ program isn't conform
with the grammar. My question was whether this was intended.
The grammar definitely deliminates the new-expression, and I
can't think of any cases where allowing a new-expression here
would cause problems.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ 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://www.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Thu, 4 Oct 2007 14:54:33 CST Raw View
Is the following program legal C++:
struct B
{
B* f() { return this ; }
} ;
int
main()
{
B* p = new B()->f() ; // line 10
return 0 ;
}
I'm particularly interested in line 10. Most compilers (but not
g++) seem to accept it, and it seems reasonable to accept it,
but formally, the standard says that the left operand of -> must
be a postfix-expression, and of course, a new-expression is not
a postfix-expression. So is it intentional that this is
illegal, and most compilers get it wrong, or is it an accidental
side effect of the way the standard is formulated, and most
compilers implement the actual intent. (The point, of course,
is that a new-expression is delimited by the keyword new on the
left, and by the limitations on the structure of a new-type-id
and the new-initializer on the right, so the above really
creates no ambiguity nor parsing problem.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ 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://www.comeaucomputing.com/csc/faq.html ]