title: Compile-time loop optimisation
body: <p>I am finding it a bit hard to understand why the following results in a compile time calculation. 
  I have read <a href="https://akrzemi1.wordpress.com/2011/05/11/parsing-strings-at-compile-time-part-i/" rel="nofollow noreferrer">this</a>, <a href="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options" rel="nofollow noreferrer">this</a>, <a href="https://www.codingame.com/playgrounds/2205/7-features-of-c17-that-will-simplify-your-code/constexpr-if" rel="nofollow noreferrer">this</a> and 
  a lot more questions on stackoverflow that tell me the following code( at least to my understanding) should not be computed at compile time because of the while loop (The code is just an example to illustrate the question):</p>
  
  <pre><code>template&lt; unsigned N &gt;
  constexpr unsigned isStringNice(const char (&amp;arr)[N], unsigned pos = 0)
  {
      //we do not like the 'D' char :)
      int currPos = 0;
      while(currPos &lt; N){
          if(arr [currPos] == 'D'){
              throw 1;
          }
          currPos ++;
      }
      return 1;
  }
  constexpr unsigned isIdxValid( unsigned idx, unsigned len){
      return idx &gt;= len?  throw 1 : idx;
  }
  
  template&lt; unsigned N &gt;
  constexpr char nth_char(const char (&amp;arr)[N], unsigned pos){
    return  isStringNice(arr),isIdxValid(pos, N),arr[pos];
  }
  
  int main(){
  
    constexpr char b = nth_char("ABC", 2);
  
    return b;
  }
  </code></pre>
  
  <p>this outputs with no flag the following <a href="https://godbolt.org/" rel="nofollow noreferrer">assembly</a> code (gcc 8.2 , thank you Godbolt)
  main:</p>
  
  <pre><code>push    rbp
  mov     rbp, rsp
  mov     BYTE PTR [rbp-1], 67
  mov     eax, 67
  pop     rbp
  ret
  </code></pre>
  
  <p>and with -O3</p>
  
  <pre><code>main:
          mov     eax, 67
          ret
  </code></pre>
  
  <p>Notice as there is no jump in there, no branch on the while condition, nothing. I had the impression that <a href="https://www.codeproject.com/Articles/857354/Compile-Time-Loops-with-Cplusplus-Creating-a-Gener" rel="nofollow noreferrer">for-loops</a> and while loops <a href="https://stackoverflow.com/questions/6872919/compile-time-loops">were not possible</a> to be evaluated at compile time. However, the compiler (gcc 8.2) evaluates the result at compile time. 
  My only thought is this happens because of loop unrolling, so i tried using <code>-fno-unroll-loops</code>, but this results in the same assembly code.On the other hand from my experience,  this flag is more like a compiler suggestion than a guarantee and gcc might still unroll the loop even though the flag is set.</p>
  
  <p><strong>Short version of my question</strong>: How come my while loop in a constexpr function is evaluated at compile time? </p>