[cfe-dev] Bug in template parsing?

Jordan Rose jordan_rose at apple.com
Fri Mar 8 09:08:43 PST 2013


On Mar 8, 2013, at 3:41 , Nicola Gigante <nicola.gigante at gmail.com> wrote:

> Hello.
> 
> I'm not sure why this piece of code doesn't compile, but I suspect a bug.
> This happens with the clang trunk (r176686) (C++11 mode)
> 
> The compilation succeeds if value is made const static OR if I put parenthesis around test<1, N>::value.
> 
> template<int I, int N>
> struct test
> {
>    bool value = test<1, N>::value;
> };
> 
> template<int N>
> struct test<1, N>
> {
>    static const bool value = true;
> };
> 
> $ ./llvm/build/bin/clang++ -std=c++11 -fsyntax-only test.cpp
> test.cpp:4:26: error: declaration of 'N' shadows template parameter
>    bool value = test<1, N>::value;
>                         ^
> test.cpp:1:21: note: template parameter is declared here
> template<int I, int N>
>                    ^
> test.cpp:4:27: error: expected ';' at end of declaration list
>    bool value = test<1, N>::value;
>                          ^
>                          ;
> test.cpp:4:24: error: expected '>'
>    bool value = test<1, N>::value;
>                       ^
> 3 errors generated.
> 
> Am I missing something (or is it already known)?

Clang is parsing this as:

bool value = (test < 1), N > ::value;

The first error is because it thinks you're declaring "bool N", the second because "> ::value" isn't a valid initializer for "bool N", and the third because "test < 1" isn't a complete template reference.

I can't be sure whether this is correct, and clearly we could have better recovery, but it explains what's going on here. I think it's due to the fact that we don't know if "test" is a template until after it's been parsed—it could be an integer that we're trying to compare against 1.

At the very least, though, please file a bug for better error recovery.

Jordan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130308/720a0140/attachment.html>


More information about the cfe-dev mailing list