title: Addition operator for types with common operator() methods
body: <p>I have two structs <code>A</code> and <code>B</code> for whom I've defined an <code>operator()(int x)</code> which returns a <code>std::vector&lt;int&gt;</code>. In my templated calling function <code>f&lt;C&gt;(int x)</code>, I'd like to be able to overload the addition operator <code>+</code> for the types <code>A</code> and <code>B</code> to mean the concatenation of the vectors returned by their <code>operator()(int x)</code> methods. How can I accomplish this?</p>
  <pre><code>#include &lt;vector&gt;
  #include &lt;fmt/ranges.h&gt;
  
  struct A {
      std::vector&lt;int&gt; operator()(int x) {
          std::vector&lt;int&gt; v = {1*x, 2*x};
          return v;
      }
  };
  
  struct B {
      std::vector&lt;int&gt; operator()(int x) {
          std::vector&lt;int&gt; v = {3*x, 4*x};
          return v;
      }
  };
  
  template &lt;typename C&gt;
  void f(int x) {
      fmt::print(&quot;{}\n&quot;, C()(x));
  }
  
  int main() {
      using C = A + B; // preferred syntax
      f&lt;C&gt;(1); // expected output: [1,2,3,4]
  }
  </code></pre>
  <p>Post-Comment Addition:</p>
  <p>As perhaps alluded to by the commenters, if I get rid of the templated function, I can certainly do this, and it works,</p>
  <pre><code>#include &lt;vector&gt;
  #include &lt;fmt/ranges.h&gt;
  #include &lt;functional&gt;
  
  struct A {
      std::vector&lt;int&gt; operator()(int x) {
          std::vector&lt;int&gt; v = {1*x, 2*x};
          return v;
      }
  };
  
  struct B {
      std::vector&lt;int&gt; operator()(int x) {
          std::vector&lt;int&gt; v = {3*x, 4*x};
          return v;
      }
  };
  
  using FUNC = std::function&lt;std::vector&lt;int&gt;(int)&gt;;
  
  FUNC operator+(FUNC &amp;f1, FUNC &amp;f2) {
      return [f1, f2](int x) {
          std::vector&lt;int&gt; v1 = f1(x);
          std::vector&lt;int&gt; v2 = f2(x);
          std::vector&lt;int&gt; v3;
          v3.reserve(v1.size() + v2.size());
          v3.insert(v3.end(), v1.begin(), v1.end());
          v3.insert(v3.end(), v2.begin(), v2.end());
          return v3;
      };
  }
  
  void f(FUNC &amp;F, int x) {
      fmt::print(&quot;{}\n&quot;, F(x));
  }
  
  int main() {
      FUNC F1 = A();
      FUNC F2 = B();
      auto F = F1 + F2;
      f(F, 1);
  }
  </code></pre>
  <p>but I want to know <em>definitively</em> if operator overloading for types ala <code>using C = A + B;</code> is even possible in <code>c++</code>?</p>