title: How to create a function that forwards its arguments to fmt::format keeping the type-safeness?
body: <p>I have two broadly related questions.</p>
  <p>I want to make a function that forwards the arguments to <code>fmt::format</code> (and later to <code>std::format</code>, when the support increases). Something like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;fmt/core.h&gt;
  
  constexpr auto my_print(auto&amp;&amp; fmt, auto&amp;&amp;... args) {
      // Error here!
      //         ~~~~~~~~v~~~~~~~~
      return fmt::format(fmt, args...);
  }
  
  int main() {
      std::cout &lt;&lt; my_print(&quot;{}&quot;, 42) &lt;&lt; std::endl;
  }
  </code></pre>
  <p>Tested with gcc 11.1.0:</p>
  <pre><code>In instantiation of ‘constexpr auto my_print(auto:11&amp;&amp;, auto:12&amp;&amp; ...) [with auto:11 = const char (&amp;)[3]; auto:12 = {int}]’:
  error: ‘fmt’ is not a constant expression
  </code></pre>
  <p>And tested with clang 12.0.1:</p>
  <pre><code>error: call to consteval function 'fmt::basic_format_string&lt;char, int &amp;&gt;::basic_format_string&lt;char [3], 0&gt;' is not a constant expression
  </code></pre>
  <p>In the library (<a href="https://github.com/fmtlib/fmt/blob/fb19faa31bc35ad2fd5ae8b630d7534fbe7bcf95/include/fmt/core.h#L2912" rel="noreferrer">core.h</a>) it's declared something like this:</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;typename... T&gt;
  auto format(format_string&lt;T...&gt; fmt, T&amp;&amp;... args) -&gt; std::string {
    // ...
  }
  </code></pre>
  <p>The problem is that <a href="https://en.cppreference.com/w/cpp/utility/format/format" rel="noreferrer">cppreference</a> indicates that the type of the first parameter is unspecified. So</p>
  <ul>
  <li>How can I make a function like <code>my_print</code> that passes the arguments to <code>fmt::format</code> and still catches the same kind of errors? Is there a more general way to do this for any kind of function?</li>
  <li>How can I infer the type of a parameter of a function like <code>std::format</code>?</li>
  </ul>
  <hr />
  <p>For more context, I want to make a function that calls to <code>std::format</code> conditionally, avoiding the formatting at all if the string won't be needed. If you know a better way to make this leave a comment, I'll be very greatful. However, my question about how to solve the general problem still stands.</p>