On Oct 9, 2013, at 1:03 PM, Richard Trieu <[email protected]> wrote:
> Here is the code in question, template parameters fixed now:
>
> template <int value>
> int sum() {
> return value + sum<value/2>;
> }
>
> template<>
> int sum<1>() { return 1; }
>
> template<int x, int y>
> int calculate_value() {
> if (x != y)
> return sum<x - y>();
> else
> return 0;
> }
>
> int value = calculate_value<1,1>();
>
> calculate_value<1,1>() causes the instantiation of sum<0>(), even though it
> is protected from being called by the conditional "if (x != y)". sum<0>() is
> the only self-recursive function here. Are you saying we should warn on that
> even though the programmer added the proper check to prevent it from being
> called?
I don’t really see this as a special case. ‘x’ and ‘y’ could be constants
defined in some other way in non-templated code. A solution to that problem is
to prune branches in the CFG, which is something we already do for many cases
where the branch conditions are easy to resolve.
Unlike some other dataflow techniques where we wanted a warning to only apply
for all template instantiations, warning for a particular template
instantiation has goodness here, and seems no real different to me than
handling other functions. There are details to be handled here: constant
folding, reporting useful diagnostics that let’s someone understand that the
recursion happens under a particular instantiation, only emitting one warning
instead of N for each instantiation, and so on. But these don’t seem like
insurmountable problems.
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits