title: Forcing a constant expression to be evaluated during compile-time?
body: <p>A few days ago I asked by which criteria the compiler decides whether or not, to compute a constexpr function during compile time.</p>
  
  <p><a href="https://stackoverflow.com/questions/14248235/when-does-a-constexpr-function-get-evaluated-at-compile-time">When does a constexpr function get evaluated at compile time?</a> </p>
  
  <p>As it turns out, a constexpr is only evaluated during compile-time, if all parameters are constant expressions and the variable you are assigning it to is are constant expression as well. </p>
  
  <pre><code>template&lt;typename base_t, typename expo_t&gt;
  constexpr base_t POW(base_t base, expo_t expo)
  {
      return (expo != 0 )? base * POW(base, expo -1) : 1;
  }
  
  template&lt;typename T&gt;
  void foobar(T val)
  {
      std::cout &lt;&lt; val &lt;&lt; std::endl;
  }
  
  int main(int argc, char** argv)
  {
      foobar(POW((unsigned long long)2, 63));
      return 0;
  }
  </code></pre>
  
  <p>If what I was told is true, this code example is very impratical, since foobar doesn't take a constexpr (you can't use consexpr for parameters for some reason), POW gets evaluated during runtime, even though it would have been possible to compute it during compile-time. The obvious solution for forcing a compile-time evaluation would be this:</p>
  
  <pre><code>auto expr = POW((unsigned long long)2, 63);
  foobar(expr);
  </code></pre>
  
  <p>This however forces me to use an additional line of code, which shouldn't be necessary each time I want to make sure a constexpr gets evaluated during compile-time. To make this a little more convenient, I've come up with the following dubious macro:</p>
  
  <pre><code>#define FORCE_CT_EVAL(func) [](){constexpr auto ___expr = func; return std::move(___expr);}()
  foobar(FORCE_CT_EVAL(POW((unsigned long long)2, 63)));
  </code></pre>
  
  <p>Despite the fact that it works just fine, I feel like as if something isn't right about it. Does creating an anonymous lambda impact performance? Does returning by rvalue reference actually move the expression to the function parameter? How does std::move impact performance? Is there a better one liner solution for this?</p>