This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Funnies about C++ parsing - unhelpful error messages
- From: Richard Guenther <rguenth at tat dot physik dot uni-tuebingen dot de>
- To: gcc at gcc dot gnu dot org
- Date: Tue, 31 Aug 2004 12:12:13 +0200 (CEST)
- Subject: Funnies about C++ parsing - unhelpful error messages
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/