title: Implement Clip() in Eigen
body: <p>I have code that clips some value to be between a range centered around 0 like below.</p>
  
  <pre><code>Eigen::VectorXd a;
  Eigen::VecotrXd b;
  a = a.cwiseMin(b).cwiseMax(-b);  // no temporary created here?
  </code></pre>
  
  <p>I want to factor out the logic into a function.
  One solution:</p>
  
  <pre><code>Eigen::VectorXd Clip(const Eigen::VectorXd&amp; a, const Eigen::VectorXd&amp; b);
  
  a = Clip(a, b);
  </code></pre>
  
  <p>But I assume this is inefficient as it creates an extra temporary?</p>
  
  <p>Another solution:</p>
  
  <pre><code>void Clip(Eigen::Ref&lt;Eigen::VectorXd&gt; a, const Eigen::VectorXd&amp; b) {
    a = a.cwiseMin(b).cwiseMax(-b);
  }
  </code></pre>
  
  <p>But this seems inconvenient to use sometimes:</p>
  
  <pre><code>void SomeFunctionSignatureICannotChange(const Eigen::VectorXd&amp; a, const Eigen::VectorXd&amp; b) {
    // Eigen::VectorXd a_clipped = Clip(a, b); would be cleaner.
    Eigen::VectorXd a_clipped;
    Clip(a_clipped, b);
  }
  </code></pre>
  
  <p>The best solution I can think of:</p>
  
  <pre><code>template &lt;typename DerivedV, typename DerivedB&gt;
  auto Clip(const Eigen::ArrayBase&lt;DerivedV&gt;&amp; v,
            const Eigen::ArrayBase&lt;DerivedB&gt;&amp; bound)
            -&gt; decltype(v.min(bound).max(-bound)) {
    return v.min(bound).max(-bound);
  }
  </code></pre>
  
  <p>(I assume 'auto' in this case is fine and not the one that <a href="https://eigen.tuxfamily.org/dox/TopicPitfalls.html" rel="nofollow noreferrer">common pitfalls</a> warned against?)</p>
  
  <p>However, the code seems template-heavy and a bit-complicated. For example, trailing return type is discouraged by google style guide <a href="https://google.github.io/styleguide/cppguide.html#trailing_return" rel="nofollow noreferrer">here</a>:</p>
  
  <blockquote>
    <p>Use the new trailing-return-type form only in cases where it's
    required (such as lambdas) or where, by putting the type after the
    function's parameter list, it allows you to write the type in a much
    more readable way. The latter case should be rare; it's mostly an
    issue in fairly complicated template code, which is discouraged in
    most cases.</p>
  </blockquote>
  
  <p>Alternatively, if I remove the trailing return type, function return type deduction will kick in. But google style guide seems to also discourage function return type deduction in public headers <a href="https://google.github.io/styleguide/cppguide.html#Type_deduction" rel="nofollow noreferrer">here</a></p>
  
  <blockquote>
    <p>Furthermore, use it only if the function or lambda has a very narrow
    scope, because functions with deduced return types don't define
    abstraction boundaries: the implementation is the interface. In
    particular, public functions in header files should almost never have
    deduced return types.</p>
  </blockquote>
  
  <p>I'm new to Eigen and C++ so not sure if I missed anything. Hope to learn from everyone's comments and suggestions. Thanks!</p>