This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Bug report


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]