title: Guidelines to do constexpr operator-overloading?
body: <p>Consider a simple int <code>Wrapper</code> class with overloaded multiplication <code>operator*=</code> and <code>operator*</code>. For "old-style" operator-overloading, one can define <code>operator*</code> in terms of <code>operator*=</code>, and there are even libraries like <a href="http://www.boost.org/doc/libs/1_54_0/libs/utility/operators.htm" rel="noreferrer"><strong>Boost.Operators</strong></a> and its modern incarnation <a href="https://github.com/d-frey/operators" rel="noreferrer"><strong>df.operators</strong></a> by @DanielFrey that reduce the boilerplate for you.</p>
  
  <p>However, for compile-time computations using the new C++11 <code>constexpr</code>, this convenience disappears. A <code>constexpr operator*</code> cannot call <code>operator*=</code> because the latter modifies its (implicit) left argument. Furthermore, there is <a href="https://stackoverflow.com/q/8936549/819272"><strong>no overloading on constexpr</strong></a>, so adding an extra <code>constexpr operator*</code> to the existing <code>operator*</code> results in an overload resolution ambiguity. </p>
  
  <p>My current approach is:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct Wrap
  {
      int value;    
  
      Wrap&amp; operator*=(Wrap const&amp; rhs) 
      { value *= rhs.value; return *this; }
  
      // need to comment this function because of overloading ambiguity with the constexpr version
      // friend Wrap operator*(Wrap const&amp; lhs, Wrap const&amp; rhs)
      // { return Wrap { lhs } *= rhs; }    
  
      friend constexpr Wrap operator*(Wrap const&amp; lhs, Wrap const&amp; rhs)
      { return { lhs.value * rhs.value }; }
  };
  
  constexpr Wrap factorial(int n)
  {
      return n? factorial(n - 1) * Wrap { n } : Wrap { 1 };    
  }
  
  // want to be able to statically initialize these arrays
  struct Hold
  {
      static constexpr Wrap Int[] = { factorial(0), factorial(1), factorial(2), factorial(3) };
  };
  
  int main() 
  {
      std::cout &lt;&lt; Hold::Int[3].value &lt;&lt; "\n"; // 6
      auto w = Wrap { 2 };
      w *= Wrap { 3 };
      std::cout &lt;&lt; w.value &lt;&lt; "\n"; // 6
  }
  </code></pre>
  
  <p><a href="http://coliru.stacked-crooked.com/view?id=e7bd9a66c69f7aa0146cd7f8de08bd73-4633dfbb9751bd61e16305d67167e3f8" rel="noreferrer">Live output here</a>. My problems with this are:</p>
  
  <ul>
  <li>duplication of the multiplication logic in both <code>operator*=</code> and <code>operator*</code>, instead of <code>operator*</code> being expressed in terms of <code>operator*=</code></li>
  <li>hence, Boost.Operators no longer works to reduce the boilerplate for writing many other arithmetic operators</li>
  </ul>
  
  <p><strong>Question</strong>: is this the recommended C++11 way of having both a run-time <code>operator*=</code> and mixed run-time/compile-time <code>constexpr operator*</code>? Does C++14 change anything here to e.g. reduce the logic duplication?</p>
  
  <p><strong>UPDATE</strong>: The answer by @AndyProwl is accepted as idiomatic but as per suggestion of @DyP, in C++11 one <em>could</em> reduce the logic duplication at the expense of an extra assignment and counter-intuitive style</p>
  
  <pre><code>    // define operator*= in terms of operator*
      Wrap&amp; operator*=(Wrap const&amp; rhs) 
      { *this = *this * rhs; return *this; }
  </code></pre>