title: Enforcing const-correctness when converting lambdas to std::function
body: <p>I have a function that takes in a <code>std::function</code> as a parameter. However, I want to ensure the function being passed in is not allowed to modify the parameters passed to it.</p>
  
  <p>Here's the simple version of the function (note that <code>T</code> can be, and usually is, a reference):</p>
  
  <pre><code>template &lt;class T&gt;
  void Bar(std::function&lt;void(std::add_const_t&lt;T&gt;)&gt; func)
  {
      // ...
  }
  </code></pre>
  
  <p>Bad usage:</p>
  
  <pre><code>Bar&lt;int&amp;&gt;([](int&amp;) { /* Do nasty stuff! */ }); /* A-OK! */
  </code></pre>
  
  <p>I want to disallow that usage, but this code compiles perfectly fine, even though I feel like it shouldn't.</p>
  
  <p>Interesting thing is, if I get rid of the template parameter, i.e.:</p>
  
  <pre><code>void Bar(std::function&lt;void(const int&amp;)&gt; func)
  {
      // ...
  }
  </code></pre>
  
  <p>Then, this usage wouldn't compile (as it shouldn't):</p>
  
  <pre><code>Bar([](int&amp;) { /* Do nasty stuff! */ }); /* Error C2664 */
  </code></pre>
  
  <p>How can I enforce this and still keep the template parameter?</p>