title: Unify C++ templates for pointers, values and smart pointers
body: <p>My real example is quite big, so I will use a simplified one. Suppose I have a data-type for a rectangle: </p>
  
  <pre><code>struct Rectangle {
    int width;
    int height;
  
    int computeArea() {
      return width * height;
    }
  }
  </code></pre>
  
  <p>And another type that consumes that type, for example: </p>
  
  <pre><code>struct TwoRectangles {
    Rectangle a;
    Rectangle b;
    int computeArea() {
      // Ignore case where they overlap for the sake of argument!
      return a.computeArea() + b.computeArea();
    }
  };
  </code></pre>
  
  <p>Now, I don't want to put ownership constraints on users of <code>TwoRectangles</code>, so I would like to make it a template: </p>
  
  <pre><code>template&lt;typename T&gt;
  struct TwoRectangles {
    T a;
    T b;
    int computeArea() {
      // Ignore case where they overlap for the sake of argument! 
      return a.computeArea() + b.computeArea();
    }
  };
  </code></pre>
  
  <p>Usages: </p>
  
  <pre><code>TwoRectangles&lt;Rectangle&gt; x;
  TwoRectangles&lt;Rectangle*&gt; y;
  TwoRectangles&lt;std::shared_ptr&lt;Rectangle&gt;&gt; z;
  // etc... 
  </code></pre>
  
  <p>The problem is that if the caller wants to use pointers, the body of the function should be different: </p>
  
  <pre><code>template&lt;typename T&gt;
  struct TwoRectangles {
    T a;
    T b;
    int computeArea() {
      assert(a &amp;&amp; b);
      return a-&gt;computeArea() + b-&gt;computeArea();
    }
  };
  </code></pre>
  
  <p>What is the best way of unifying my templated function so that the maxiumum amount of code is reused for pointers, values and smart pointers?</p>