Bug: Parse error or C++ language ambiguity?

Charles Durst cdurst@world.std.com
Tue Oct 6 17:04:00 GMT 1998


Description:

When I try to compile the following simplified example with EGCS 1.1b,
EGCS 1.0, or even GCC 2.8.1, I get the error messages listed below.

I feel that line 14 should be considered an unambiguous attempt to
create a temporary object of type "A" using the return value of the
member function "foo()" and then to call the "A::print" member function
on it.

The problem (as can be seen by uncommenting line 1) is that GCC mistakes
it for a declaration of a global function named "foo".

I'll certainly admit that if the ".print()" function call were not
there, it would be an ambiguous C++ statement.

Clearly GCC thinks it has correctly classified the statement as a
declaration before reaching the ".print()" part.  Other C++ compilers we
use seem to be able to understand this.

If you decide to treat this as a non-bug, then please consider adding
this to the "Non-Bugs" section of the GCC Info documentation.  Thanks.


The entire source file (try.cxx):

    //void foo();  // If commented out, only the parse error is reported

    class A {
    public:
        A(const A&);
        void print();
    };
    class B {
    public:
        const A& foo();
        void bar();
    };
    void B::bar() {
        A(foo()).print();  // Line 14
    }


The compiler messages:

    $ g++ -c try.cxx
    try.cxx: In method `void B::bar()':
    try.cxx:14: parse error before `.'

    # If line 1 is uncommented the messages are a little more clear:

    $ g++ -c try.cxx
    try.cxx: In method `void B::bar()':
    try.cxx:14: warning: extern declaration of `class A foo()' doesn't match
    try.cxx:1: warning: global declaration `void foo()'
    try.cxx:14: parse error before `.'

OS Version:
    Red Hat Linux 5.1 for Intel (EGCS 1.1b and EGCS 1.0)
    SunOS 4.1.3_U1 on sun4m     (GCC 2.8.1)


Charles Durst          .------------------------------------------------.
=======================|  cdurst@world.std.com                          |==
                       |  http://www.tiac.net/users/cdurst/cdurst.html  |
                       `------------------------------------------------'
P.S. - I am not subscribed to either of these lists, so I apologize if
this has already been reported and request that you please CC: me on any
further discussions of this bug report.  Thanks.



More information about the Gcc-bugs mailing list