title: using static_assert in a class - how?
body: <p>I'm trying to make it simple to check at compile time whether the conversion of one value to a new type preserves the value. There may be something in the STL that can do this for me, but I don't see it, so I wrote one. It works, but I wanted to convert it to a class in order to make the use a little simpler for others. I can't get that to work and I feel like I'm missing something obvious.</p>
  
  <p>This works fine:</p>
  
  <pre><code>    template &lt;typename T, typename Q&gt;
      constexpr bool checkV(const Q x) 
      {return x == (T)x &amp;&amp; ((x &lt; 0) == ((T)x &lt; 0));}
  
      static_assert(checkV&lt;unsigned int&gt;(-7), "No");
  </code></pre>
  
  <p>But the call is ungainly, so I wanted something more like</p>
  
  <pre><code>    CheckIt&lt;unsigned int&gt;(-7)
  </code></pre>
  
  <p>and so I attempted</p>
  
  <pre><code>    template&lt;typename T&gt;
      class CheckIt {public:
        template&lt;typename Q&gt;
        constexpr CheckIt(const Q x) {static_assert(checkV&lt;T&gt;(x), "No");}
  };
  </code></pre>
  
  <p>I've tried various combinations of const and constexpr (and discovered parameters can't be constexprs, which is annoying). Everything I try results in g++ complaining that x is not a constant expression in the constructor.</p>
  
  <p>What am I missing? checkv, and the constructor, are both always called with constants, like -7. checkv() is happy to evaluate at compile time and I can't see where using a constructor function to wrap that adds any additional burden to the compiler, but obviously it does. Note I need this to work in C++11, but I don't see where later versions will help. I have no problems checking at runtime, with assert, but I want the compile time solution for constants. TIA.</p>