title: Why does gcc complain about my loops?
body: <p>I have some fairly trivial code, still gcc complains (in <code>-O3 -march=native</code>) about loop unrolling:</p>
  
  <pre><code>cannot optimize loop, the loop counter may overflow [-Wunsafe-loop-optimizations]
  for(auto&amp; plan : fw)
  ^
  </code></pre>
  
  <p>Here is a (stripped of all fftw stuff, else it would be quite long) version of my code</p>
  
  <pre><code>class FFTWManager
  {
      public:
          void setChannels(unsigned int n)
          {
              fw.resize(n);
              bw.resize(n);
                              //some fftw-specific stuff comes here
          }
  
          void forward()
          {
              for(auto&amp; plan : fw)
                  fftw_execute(plan);
          }
  
          void backward()
          {
              for(auto&amp; plan : bw)
                  fftw_execute(plan);
          }
  
      private:
          std::vector&lt;fftw_plan&gt; fw = {};
          std::vector&lt;fftw_plan&gt; bw = {};
  };
  </code></pre>
  
  <p>The vectors never exceed a size of 2 in my code.</p>
  
  <hr>
  
  <p>Edits according to comments : 
  I use a lot of flags.</p>
  
  <p><code>-pedantic -Wextra -Weffc++  -Wall -Wcast-align  -Wcast-qual  -Wchar-subscripts  -Wcomment -Wconversion  -Wdisabled-optimization    -Wformat  -Wformat=1  -Wformat-nonliteral -Wformat-security   -Wformat-y2k  -Wimport  -Winit-self  -Winline  -Winvalid-pch    -Wunsafe-loop-optimizations  -Wmissing-braces  -Wmissing-field-initializers -Wmissing-format-attribute    -Wmissing-include-dirs -Wmissing-noreturn  -Wpacked  -Wparentheses  -Wpointer-arith  -Wredundant-decls -Wreturn-type  -Wsequence-point  -Wshadow -Wsign-compare  -Wstack-protector -Wstrict-aliasing=3 -Wswitch  -Wswitch-default  -Wswitch-enum -Wtrigraphs  -Wuninitialized  -Wunknown-pragmas  -Wunreachable-code -Wunused  -Wunused-function  -Wunused-label  -Wunused-parameter  -Wunused-value  -Wunused-variable  -Wvariadic-macros  -Wvolatile-register-var  -Wwrite-strings</code> </p>
  
  <p>I don't see the point of speaking of putting infos about <code>fftw_execute</code> here but if you want to see the whole code (that I judged too long for a SO post), it's here :
  <a href="https://github.com/jcelerier/watermarking/blob/master/src/libwatermark/transform/FFTWManager.h" rel="noreferrer">https://github.com/jcelerier/watermarking/blob/master/src/libwatermark/transform/FFTWManager.h</a></p>
  
  <p>GCC : <code>gcc version 4.8.2 (Debian 4.8.2-10)</code></p>
  
  <p>I don't see why changing from unsigned int to size_type would change anything since I don't get any warning in my <code>setChannels</code> method (even if I think it's long unsigned int on my platform) and once the size is set, the original type of the variable that was used to set it seems quite irrelevant to me.</p>
  
  <p>There is no warning with the basic <code>for(int i = 0; i &lt; bw.size(); i++)</code> or with the iterator version <code>for(auto i = bw.begin(); i != bw.end(); i++)</code>.</p>
  
  <p>I also tried with clang, which seems to recognize the warning swich so I guess they also implemented the optimization, and I don't get any warnings (but much quicker compile times \o)</p>
  
  <p>Sorry about long feedback, I was out. </p>