title: compile-time conversions of floating point constants
body: <p>This question refers to my previous question: <a href="https://stackoverflow.com/questions/16584191/float-conversions-in-templates">float conversions in templates</a></p>
  
  <p>I would like to prevent a run-time conversion of floating-point constants. The prevailing view taken in my previous question was, that, say, a <code>float(.5)</code> conversion is allowed to take place at run-time. But how about:</p>
  
  <pre><code>template &lt;typename A, typename B&gt;
  constexpr A convert(B const a)
  {
    return a;
  }
  </code></pre>
  
  <p>An assert to guarantee the compile-time evaluation of a <code>constexpr</code> function is discussed here:
  <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>Is a <code>constexpr</code> + <code>assert</code> combination the only guaranteed way to accomplish such conversions at compile-time?</p>
  
  <p>SOLUTION:</p>
  
  <p>After a lot of head-scratching, I've come to the conclusion, that the <code>convert</code> function I've provided is unnecessary. The best I could come with was:</p>
  
  <pre><code>#define CONVERT(T, V) static constexpr T const T##_##V(V)
  
  int main()
  {
    CONVERT(float, 1);
  
    ::std::cout &lt;&lt; float_1 &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>The best alternative would be a <code>floating_point_constant</code> counterpart of <code>::std::integral_constant</code>, but alas, it is not possible to write one.</p>