title: Overloading multiple function objects by reference
body: <p>In C++17, it is trivial to implement an <code>overload(fs...)</code> function that, given any number of arguments <code>fs...</code> satisfying <a href="http://en.cppreference.com/w/cpp/concept/FunctionObject" rel="noreferrer"><code>FunctionObject</code></a>, returns a new <em>function object</em> that behaves like an overload of <code>fs...</code>. Example:</p>
  
  <pre><code>template &lt;typename... Ts&gt;
  struct overloader : Ts...
  {
      template &lt;typename... TArgs&gt;
      overloader(TArgs&amp;&amp;... xs) : Ts{forward&lt;TArgs&gt;(xs)}...
      {
      }
  
      using Ts::operator()...;
  };
  
  template &lt;typename... Ts&gt;
  auto overload(Ts&amp;&amp;... xs)
  {
      return overloader&lt;decay_t&lt;Ts&gt;...&gt;{forward&lt;Ts&gt;(xs)...};
  }
  
  int main()
  {
      auto o = overload([](char){ cout &lt;&lt; "CHAR"; }, 
                        [](int) { cout &lt;&lt; "INT";  });
  
      o('a'); // prints "CHAR"
      o(0);   // prints "INT"
  }
  </code></pre>
  
  <p><a href="https://wandbox.org/permlink/VvZtKhUEFZ7zyvoa" rel="noreferrer"><strong>live example on wandbox</strong></a></p>
  
  <hr>
  
  <p>Since the above <code>overloader</code> inherits from <code>Ts...</code>, it needs to either copy or move the <em>function objects</em> in order to work. <strong>I want something that provides the same overloading behavior, but only references to the passed function objects.</strong></p>
  
  <p>Let's call that hypothetical function <code>ref_overload(fs...)</code>. My attempt was using <code>std::reference_wrapper</code> and <code>std::ref</code> as follows:</p>
  
  <pre><code>template &lt;typename... Ts&gt;
  auto ref_overload(Ts&amp;... xs)
  {
      return overloader&lt;reference_wrapper&lt;Ts&gt;...&gt;{ref(xs)...};
  }
  </code></pre>
  
  <p>Seems simple enough, right? </p>
  
  <pre><code>int main()
  {
      auto l0 = [](char){ cout &lt;&lt; "CHAR"; };
      auto l1 = [](int) { cout &lt;&lt; "INT";  };
  
      auto o = ref_overload(l0, l1);
  
      o('a'); // BOOM
      o(0);
  }
  </code></pre>
  
  <p></p>
  
  <pre><code>error: call of '(overloader&lt;...&gt;) (char)' is ambiguous
   o('a'); // BOOM
        ^
  </code></pre>
  
  <p><a href="https://wandbox.org/permlink/K1a1BqxCV72SbzxZ" rel="noreferrer"><strong>live example on wandbox</strong></a></p>
  
  <p>The reason it doesn't work is simple: <a href="http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator()" rel="noreferrer"><code>std::reference_wrapper::operator()</code></a> is a <em>variadic function template</em>, which <strong>does not play nicely with overloading</strong>.</p>
  
  <p>In order to use the <code>using Ts::operator()...</code> syntax, I need <code>Ts...</code> to satisfy <code>FunctionObject</code>. If I try to make my own <code>FunctionObject</code> wrapper, I encounter the same issue:</p>
  
  <pre><code>template &lt;typename TF&gt;
  struct function_ref
  {
      TF&amp; _f;
      decltype(auto) operator()(/* ??? */);
  };
  </code></pre>
  
  <p>Since there's no way of expressing <em>"compiler, please fill the <code>???</code> with the exact same arguments as <code>TF::operator()</code>"</em>, I need to use a <em>variadic function template</em>, solving nothing. </p>
  
  <p>I also cannot use something like <a href="http://www.boost.org/doc/libs/1_63_0/libs/type_traits/doc/html/boost_typetraits/reference/function_traits.html" rel="noreferrer"><code>boost::function_traits</code></a> because one of the functions passed to <code>overload(...)</code> may be a <em>function template</em> or an <em>overloaded function object</em> itself!</p>
  
  <p>Therefore my question is: <strong>is there a way of implementing a <code>ref_overload(fs...)</code> function that, given any number of <code>fs...</code> function objects, returns a new function object that behaves like an overload of <code>fs...</code>, but refers to <code>fs...</code> instead of copying/moving them?</strong></p>