title: Can you use decltype in a template parameter?
body: <p>I am trying to overload a multiplication operator but do not want to type out multiple overloaded functions to take into account multiplying int and float, int and double, float and int, etc... I was hoping to write one overloaded operator to account for all combinations of multiplication with floats, ints, and doubles and get the proper return type. I am getting errors saying that no operator found which takes a right-hand operand of type 'Widget::Widget' (or there is no acceptable conversion). I think this is because I am using the decltype to set the template type of the return object, the Widget. Using a trailing return type works if the return is not a template object. </p>
  
  <p>Here is an example of the overloaded operator I am trying to make:</p>
  
  <pre><code>template&lt;typename T1, typename T2&gt;
  auto
  operator*(const Widget&lt;T1&gt;&amp; aWidge, const Widget&lt;T2&gt;&amp; bWidge) -&gt; Widget&lt;decltype(aWidge.x*bWidge.x)&gt;
  {
      Widget&lt;decltype(aWidge.x*bWidge.x)&gt; result;
  
      //do stuff needed when multiplying two Widgets
  
      return result;
  }
  
  template&lt;typename T&gt;
  Widget&lt;T&gt;&amp; Widget&lt;T&gt;::operator=(const Widget&lt;T&gt;&amp; aWidget)
  {
      x = aWidget.x;
  
      return *this;
  }
  </code></pre>
  
  <p>And here is an example of the template class</p>
  
  <pre><code>template&lt;typename T&gt; class Widget
  {
      private:
          T x;
      public:
          Widget();
          ~Widget();
          void SetX(T value);
          Widget&lt;T&gt;&amp; operator=(const Widget&lt;T&gt;&amp; aWidget);
  }
  </code></pre>
  
  <p>Example Main.cpp</p>
  
  <pre><code>int main()
  {
      Widget&lt;int&gt; aWidge;
      Widget&lt;float&gt; bWidge;
      Widget&lt;float&gt; cWidge;
  
      aWidge.SetX(2);
      bWidge.SetX(2.0);
  
      cWidge = aWidge*bWidge; //this should give a float return type
  }
  </code></pre>