This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Bug report
- To: chicare at ibm dot net
- Subject: Re: Bug report
- From: Alexandre Oliva <oliva at dcc dot unicamp dot br>
- Date: 15 Sep 1998 21:39:29 +-300
- Cc: egcs <egcs-bugs at cygnus dot com>
- References: <35FDD8B1.E46FD962@ibm.net>
Greg Chicares <chicare@ibm.net> writes:
> template <int N> class Factorial { public: enum {value = N *
> Factorial < N - (N == 0) ? N :(0<N && N<=12) ? 1 : N > ::value }; };
> int main() { Factorial<3> f3; cout << f3.value << endl; }
> // Result of compiling as
> // g++ bug00.cpp
> // bug00.cpp:14: parse error before `}'
> // bug00.cpp:14: missing ';' before right brace
> // bug00.cpp:15: parse error before `}'
> // bug00.cpp: In function `int main()':
> // bug00.cpp:19: `class Factorial<3>' has no member named `value'
> // Windows 95 OSR2, using MingW32
This code snippet reveals one real bug of egcs, but the code is buggy
too. The problem is that operator- has precedence over ?:, so this is
evaluated as `(N - (N==0)) ? N : ...', which is not what you want.
Anyway, the parse error is still unjustified. egcs does not notice it
is within a template argument when it parses `N >', so it thinks the
`>' is a greater-than operator. Unfortunately, fixing this problem is
quite hard, or so I thought last time I decided to take a look at it.
Maybe Mark Mitchell's new C++ parser will handle this better? Mark?
Fortunately, working around this problem very easy: just surround the
expression after operator- with parentheses:
> Factorial < N -((N == 0) ? N :(0<N && N<=12) ? 1 : N)> ::value }; };
And egcs now says:
test.cc: In instantiation of `Factorial<-1>':
test.cc:16: instantiated from `Factorial<0>'
test.cc:16: instantiated from `Factorial<1>'
test.cc:16: instantiated from `Factorial<2>'
test.cc:16: instantiated from `Factorial<3>'
test.cc:16: instantiated from here
test.cc:16: incomplete type `Factorial<0>' does not have member `value'
test.cc:16: enumerator value for `value' not integer constant
And now it is correct.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil