[cfe-dev] Weird diagnostic about non-type template argument

Chandler Carruth chandlerc at google.com
Mon Aug 16 01:22:47 PDT 2010


On Mon, Aug 16, 2010 at 12:31 AM, Abramo Bagnara
<abramo.bagnara at gmail.com>wrote:

>
> The following typescript show the issue:
>
> $ cat t.cc
>
> template <int *ptr>
> struct S1 { };
>
> template <bool b>
> struct S2 { };
>
> int v1;
>
> S1<(&v1)> x;
> S2<(2>2)> y;
> $ clang -c t.cc
> t.cc:10:4: error: non-type template argument cannot be surrounded by
> parentheses
> S1<(&v1)> x;
>   ^~~~~
> 1 error generated.
>
> My notes:
>
> 1) in my opinion the diagnostic is due to a needless too strong
> interpretation of the standard:
>
> - comeau and intel does not mark this as an error
>
> - gcc seems to mark this as an error due to some bug (the diagnostic  is
> very weird)
>

The standard seems really really clear here -- the argument is *not* an
expression, it's an address. You're only provided one mechanism for writing
an address in this context: '& id-expression', with the '&' being optional
in certain cases. You can't even write '0' here. I think it's actually
useful to preclude parentheses as they make this look like an expression,
which it simply is not. You can't write '(&foo + sizeof(int))' to advance
past one element of an int array either.

2) also if there is disagreement about 1 I don't see the reason to
> generate an error instead of a warning
>

A ExtWarn seems not implausible here, but I'm not sure why supporting this
is important.


> 3) that apart the diagnostic message is misleading: non-type template
> argument might (and sometimes should, see the following line in source)
> be surrounded by parentheses
>

The second line is a completely different non-type template argument: an
integral constant-expression. There are no parentheses around the template
argument, there are parentheses as part of the constant-expression, which
allows parentheses among many other constructs. It is certainly unfortunate
that there is no way to write a '>' in an ICE without parentheses, but I
don't think that artifact necessarily warrants allowing parentheses for all
template arguments, or even for all non-type template arguments...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100816/0717c4ec/attachment.html>


More information about the cfe-dev mailing list