PR c++/26997 g++ reports misleading error message when the identifier with error occurs earlier on the same line

Mark Mitchell mark@codesourcery.com
Tue Oct 28 07:37:00 GMT 2008


Manuel López-Ibáñez wrote:

> Well, it is not clear to me (and the code does not explain) in which
> cases a function cast can be confused with a constructor and  what
> should happen in each of those cases. I have tried to build some
> testcases using such a function cast and I always get 'invalid cast to
> function type'. If someone cared to explain, I could try something
> else.

The ambiguous case is just:

  class C {};
  (C ());

This is not a cast, of course.  But, it looks like a cast -- until you
hit the semicolon.

On the other hand:

  (C ())(3);

is an invalid cast to a function type.  And, note that:

  struct S {
    void operator()(int);
  };

  (S ())(3);

is an invalid cast to the type of a function taking no arguments and
returning an S, while:

  S()(3)

is a valid call to S::operator().

This suggests that perhaps what you want is to check after parsing the
closing parenthesis of the possible cast whether the next token is a
semicolon, comma, or some other thing which cannot possibly be the start
of a cast-expression.  If it is, you're not looking at a cast.  If it is
such a thing, you can commit to the tentative parse, knowing that what
you are looking at is a cast -- even if erroneous.

So, for example:

  (X) ; // Not a cast.
  (X) 3 ... // A cast.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713



More information about the Gcc-patches mailing list