Archive pour juin 2015

boost goodie to clamp numbers

How do you clamp a number in a given range in C++ ? Classical way to do it is: double clamped = std::min(maxValue, std::max(minValue, value))double clamped = std::min(maxValue, std::max(minValue, value)) But why repeat this magic formula when boost has clamp() for you ? From now on, do: #include <boost/algorithm/clamp.hpp> double clamped = boost::algorithm::clamp(value, minValue, maxValue)#include […]

,

Pas de commentaire

Namespace, template typedef and operator overloads – The Evil trio

What an unconfortable situation ? I had the following code which would not compile. // Range.h #include &lt;boost/numeric/interval.hpp&gt; #include &lt;iostream&gt; namespace App { template &lt;typename T&gt; using Range = boost::numeric::interval&lt;T&gt;; template &lt;typename T&gt; std::ostream&amp; operator&lt;&lt;(std::ostream &amp;os, const Range&lt;T&gt; &amp;r) { … } } // namespace App     // Filter.cpp #include &quot;Range.h&quot; #include &lt;iostream&gt; namespace […]

Pas de commentaire