Re: new expression as left operand of ->
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 ]