title: rvalue reference forwarding
body: <p>I am writing a wrapper around <code>std::jthread</code> and some surrounding infrastructure. I cannot wrap my head around why the following won't compile:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;functional&gt;
  #include &lt;thread&gt;
  
  // two random functions
  void foo(int i) { std::cout &lt;&lt; &quot;foo &quot; &lt;&lt; i &lt;&lt; std::endl; }
  
  void bar(int i) { std::cout &lt;&lt; &quot;bar &quot; &lt;&lt; i &lt;&lt; std::endl; }
  
  // mechanism to identify them
  enum function_kind {
      foo_kind, bar_kind
  };
  std::map&lt;function_kind, std::function&lt;void(
          int)&gt;&gt; kind_to_function{{foo_kind, foo},
                                  {bar_kind, bar}};
  
  // wrapper around jthread
  // (additional functionality ommitted for brevity)
  template&lt;typename Callable, typename... Args&gt;
  class MyThread {
  public:
      explicit MyThread(Callable &amp;&amp;function, Args &amp;&amp;...args) : m_thread{
              std::forward&lt;Callable&gt;(function),
              std::forward&lt;Args&gt;(args)...} {}
  
  private:
      std::jthread m_thread;
  };
  
  int main() {
      std::jthread t1(kind_to_function[foo_kind], 3); // works
      MyThread t2(kind_to_function[foo_kind], 3); // complains
      return 0;
  }
  
  
  </code></pre>
  <p>I am really just trying to mimic whatever <code>std::jthread</code> is doing with my own class.</p>
  <p>The IDE (clion) complains, that the first argument to <code>t2</code> is not an rvalue. The compiler complains a little more complicated:</p>
  <pre><code>main.cpp: In function ‘int main()’:
  main.cpp:29:46: error: class template argument deduction failed:
     29 |     MyThread t2(kind_to_function[foo_kind], 3); // complains
        |                                              ^
  main.cpp:29:46: error: no matching function for call to ‘MyThread(std::map&lt;function_kind, std::function&lt;void(int)&gt; &gt;::mapped_type&amp;, int)’
  main.cpp:20:14: note: candidate: ‘MyThread(Callable&amp;&amp;, Args&amp;&amp; ...)-&gt; MyThread&lt;Callable, Args&gt; [with Callable = std::function&lt;void(int)&gt;; Args = {int}]’ (near match)
     20 |     explicit MyThread(Callable &amp;&amp;function, Args &amp;&amp;...args) : m_thread{std::forward&lt;Callable&gt;(function),
        |              ^~~~~~~~
  main.cpp:20:14: note:   conversion of argument 1 would be ill-formed:
  main.cpp:29:46: error: cannot bind rvalue reference of type ‘std::function&lt;void(int)&gt;&amp;&amp;’ to lvalue of type ‘std::map&lt;function_kind, std::function&lt;void(int)&gt; &gt;::mapped_type’ {aka ‘std::function&lt;void(int)&gt;’}
     29 |     MyThread t2(kind_to_function[foo_kind], 3); // complains
        |                                              ^
  main.cpp:18:7: note: candidate: ‘template&lt;class Callable, class ... Args&gt; MyThread(MyThread&lt;Callable, Args&gt;)-&gt; MyThread&lt;Callable, Args&gt;’
     18 | class MyThread {
        |       ^~~~~~~~
  main.cpp:18:7: note:   template argument deduction/substitution failed:
  main.cpp:29:46: note:   ‘std::function&lt;void(int)&gt;’ is not derived from ‘MyThread&lt;Callable, Args&gt;’
     29 |     MyThread t2(kind_to_function[foo_kind], 3); // complains
        |                                              ^
  </code></pre>
  <p>In any case, the arguments work for <code>std::jthread</code>, which also just takes rvalues... So what am I missing?</p>