title: constexpr parametrized function pointer
body: <p>I have the following third-party API:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>using StatisticsFunc = double (*)(const std::vector&lt;double&gt; &amp;)
  libraryClass::ComputeStatistics(StatisticsFunc sf);
  </code></pre>
  
  <p>Which I'm using like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>obj1-&gt;ComputeStatistics([](const auto&amp; v) {return histogram("obj1", v);};
  obj2-&gt;ComputeStatistics([](const auto&amp; v) {return histogram("obj2", v);};
  </code></pre>
  
  <p>But all those lambdas are just repeated code. I'd rather have it like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>obj1-&gt;ComputeStatistics(getHistogramLambda("obj1"));
  </code></pre>
  
  <p>So I need to define:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>constexpr auto getHistogramLambda(const char* name) {
      return [](const auto&amp; v) {return histogram(name, v);};
  }
  </code></pre>
  
  <p>But it won't work, because <code>name</code> is not captured. Neither will this work:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>constexpr auto getHistogramLambda(const char* name) {
      return [name](const auto&amp; v) {return histogram(name, v);};
  }
  </code></pre>
  
  <p>Because capturing lambda is not stateless anymore and cannot be cast to function pointer.</p>
  
  <p>Ofc one can do it as a macro, but I want a modern C++ 17 solution. </p>
  
  <p>Passing string as template argument seems an option as well:
  <a href="https://stackoverflow.com/a/28209546/7432927">https://stackoverflow.com/a/28209546/7432927</a> , but I'm curious if there's a <code>constexpr</code> way of doing it. </p>