https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102855
康桓瑋 <hewillk at gmail dot com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |hewillk at gmail dot com --- Comment #2 from 康桓瑋 <hewillk at gmail dot com> --- (In reply to Bernhard Manfred Gruber from comment #0) > Using `#pragma GCC unroll n` for a loop fails to compile when `n` is a > template parameter. Given the following code: > > ```c++ > void f(int); > > template <int n> > void g() { > #pragma GCC unroll n > for (int i = 0; i < n; i++) > f(i); > } > > int main() { > constexpr auto n = 100; > g<n>(); > } > ``` > g++-11.2 produces the following diagnostic: > ``` > source>: In function 'void g()': > <source>:5:24: error: '#pragma GCC unroll' requires an assignment-expression > that evaluates to a non-negative integral constant less than 65535 > 5 | #pragma GCC unroll n > | ^ > ``` > If `g` is turned into a normal function and the variable `n` is moved form > `main` to `g`, the example compiles fine and produces the expected behavior. > > Allowing `n` inside the pragma to be a template parameter helps temendously > in optimizing hot loops in templated numeric codes. Such a feature is also > supported by clang, icc and nvcc. I want to kindly ask you to provide this > additional functionality. > > Thank you! > > Example on godbolt: > https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale: > 14,fontUsePx:'0',j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:8, > positionColumn:2,positionLineNumber:8,selectionStartColumn:2, > selectionStartLineNumber:8,startColumn:2,startLineNumber:8),source: > 'void+f(int)%3B%0A%0Atemplate+%3Cint+n%3E%0Avoid+g()+%7B%0A++++%23pragma+GCC+ > unroll+n%0A++++for+(int+i+%3D+0%3B+i+%3C+n%3B+i%2B%2B)%0A++++++++f(i)%3B%0A%7 > D%0A%0Aint+main()+%7B%0A++++constexpr+auto+n+%3D+100%3B%0A++++g%3Cn%3E()%3B%0 > A%7D'),l:'5',n:'0',o:'C%2B%2B+source+%231',t:'0')),k:54.105263157894754,l: > '4',n:'0',o:'',s:0,t:'0'),(g:!((g:!((h:compiler,i:(compiler:g112,filters:(b: > '0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel: > '0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j: > 4,lang:c%2B%2B,libs:!(),options:'-O3+-Wall+-Wextra',selection:(endColumn:1, > endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1, > selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree: > '1'),l:'5',n:'0',o:'x86-64+gcc+11.2+(C%2B%2B,+Editor+%231,+Compiler+%234)',t: > '0')),k:20.000000000000004,l:'4',m:50,n:'0',o:'',s:0,t:'0'),(g:!((h:output,i: > (compiler:4,editor:1,fontScale:14,fontUsePx:'0',tree:'1',wrap:'1'),l:'5',n: > '0',o:'Output+of+x86-64+gcc+11.2+(Compiler+%234)',t:'0')),header:(),l:'4',m: > 50,n:'0',o:'',s:0,t:'0')),k:45.894736842105274,l:'3',n:'0',o:'',t:'0')),l: > '2',n:'0',o:'',t:'0')),version:4 Why your godbolt link is so Dope.