[cfe-dev] Class Template Argument Deduction failure

Arthur O'Dwyer via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 29 07:31:51 PDT 2019


On Mon, Apr 29, 2019 at 10:13 AM Nicolas Lesser via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Lorenzo Cavallini via cfe-dev wrote:
> > Hello everyone,
> > I'm writing to report what I think is an issue related to Class Template
> > Argument Deduction (CTAD for short). Compiling this short snippet:
> >
> > #include <cstdio>
> > #include <vector>
> >
> > template<typename T>
> > void printV(const std::vector<T>& v)
> > {
> >     if (v.size() == 1) {
> >         printf("%d ", v[0]);
> >         return;
> >     }
> >     int middle = v.size()/2;
> >     printV(std::vector{v.begin(), v.end()+middle});
> > }
> >
> > int main()
> > {
> >     std::vector<int> v = {10,1};
> >     printV(v);
> >     return 0;
> > }
> >
> > will result in clang generating an infinite error report (at least, I
> left
> > it running for several hours, then I stopped the process...), due to (I
> > guess) wrongly deducted template arguments. If the "printf" line is
> > removed, no error is generated but clang will compile for what it looks
> > like forever (the process is constantly at 100% and does not stop after
> > several hours).
> [...]
>
> Very interesting find! You are correct that this happens due to infinite
> template recursion, not due to a bug in the standard. [...]
> This also has infinite template recursion, but in that case, gcc and clang
> stop after a specified -ftemplate-depth. Seems like in your specific
> example this limit is somehow bypassed.
>

The limit is not bypassed. But the default -ftemplate-depth is 1024, so the
number of messages output is (1+2+3+4+...+1023+1024), and the size of each
message is O(n), so in total we're seeing about O(1024*1024*1024)
characters printed. (Except that on trunk, Clang already skips most of the
messages output, which takes the size of the output back down to
O(1024*1024) — that is, O(1024) messages each of size O(1024) characters.)

A local build of Clang trunk on my machine takes 148 seconds to finish
printing all the messages in Lorenzo's original test.

$ time $ROOT/llvm/build/bin/clang++ x.cc -std=c++17 2>&1 | wc

   18211 8373912 128703709


real 1m28.044s

user 1m26.359s

sys 0m2.073s

–Arthur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190429/18741e14/attachment.html>


More information about the cfe-dev mailing list