public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Funnies about C++ parsing - unhelpful error messages
@ 2004-08-31 10:34 Richard Guenther
  0 siblings, 0 replies; only message in thread
From: Richard Guenther @ 2004-08-31 10:34 UTC (permalink / raw)
  To: gcc

Hi!

While trying to resolve another corner case of C++ parsing
rules (Rules? Where do I have to look in the standard to understand
why compilers do not do what I think they should do?):

template <class T>
struct FooBar {};

template <class T>
struct Foo
{
        Foo(const T&) {}
};

template <class T>
struct Bar
{
        Bar(const T&) {}
        void operator()(int) {}
};

void bar(int x)
{
        (Bar<Foo<FooBar<int> > >(FooBar<int>()))(x);          // 19
        Bar<Foo<FooBar<int> > >(FooBar<int>())(x);            // 20
        Bar<Foo<FooBar<int> > >(FooBar<int>()).operator()(x); // 21
}

(yes, this nesting of templates is necessary to trigger the case)

alwazn:tests> g++-3.4 -c param.cpp
param.cpp: In function `void bar(int)':
param.cpp:19: error: invalid cast to function type `Bar<Foo<FooBar<int> >
> ()(FooBar<int> (*)())'
param.cpp:20: error: `FooBar<int>' specified as declarator-id
param.cpp:20: error: multiple declarations `int' and `Bar<Foo<FooBar<int>
> >'
param.cpp:20: error: function `int FooBar<int>()' is initialized like a
variable
alwazn:tests> icpc -strict_ansi -c param.cpp
param.cpp(19): error: cast to type "Bar<Foo<FooBar<int>>> (FooBar<int>
(*)())" is not allowed
  	(Bar<Foo<FooBar<int> > >(FooBar<int>()))(x);          // 19
  	 ^
param.cpp(20): error: an explicit template argument list is not allowed on
this declaration
  	Bar<Foo<FooBar<int> > >(FooBar<int>())(x);            // 20
  	                        ^
param.cpp(20): error: function "<error>" may not be initialized
  	Bar<Foo<FooBar<int> > >(FooBar<int>())(x);            // 20

so, line 21 is the only syntax I could come up with to invoke my
desired behavior - create an object Bar and call its operator()(int).

Does the standard not allow parsing lines 19/20 with respect to
what I want to do or is this just common deficiency in existing parsers?
Where does the standard say so?

Also, I consider the error messages for line 20 not helpful in figuring
out what is going wrong (neither from gcc, nor from icpc).  Even just
"parse error" would be less confusing here ;)

Also, what's a declarator-id?  Yes, I see it in the standards grammar
rules, but what would have been parsed as id-expression here, what
as type-name?  If we talk about declarator-id, we maybe should quote
what was matched?

Richard.

--
Richard Guenther <richard dot guenther at uni-tuebingen dot de>
WWW: http://www.tat.physik.uni-tuebingen.de/~rguenth/

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2004-08-31 10:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-31 10:34 Funnies about C++ parsing - unhelpful error messages Richard Guenther

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).